为了账号安全,请及时绑定邮箱和手机立即绑定

python的多处理模块的进程为守​​护程序时的join()

python的多处理模块的进程为守​​护程序时的join()

侃侃尔雅 2021-04-07 10:14:04
我很困惑为什么下面的代码块按其方式工作。当进程是守护程序并且不调用join()时,它何时调用join()。当它不调用join()时,似乎主进程终止,并且守护进程在主进程终止后都终止:from multiprocessing import Processimport osdef info(title):    print(title)    print('module name:', __name__)    if hasattr(os, 'getppid'):  # only available on Unix        print('parent process:', os.getppid())    print('process id:', os.getpid())def f(name):    info('function f')    print('hello', name)if __name__ == '__main__':    info('main line')    p = Process(target=f, args=('bob',))    p.daemon = True    p.start()    #p.join()输出:main linemodule name: __main__parent process: 290process id: 4793join()称为:from multiprocessing import Processimport osdef info(title):    print(title)    print('module name:', __name__)    if hasattr(os, 'getppid'):  # only available on Unix        print('parent process:', os.getppid())    print('process id:', os.getpid())def f(name):    info('function f')    print('hello', name)if __name__ == '__main__':    info('main line')    p = Process(target=f, args=('bob',))    p.daemon = True    p.start()    p.join()输出:main linemodule name: __main__parent process: 290process id: 4807function fmodule name: __main__parent process: 4807process id: 4808hello bob
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

是的,你是对的。当主进程终止时,守护进程也将终止。该页面将为您提供更多详细信息: 为什么Python多处理守护进程未打印到标准输出?


查看完整回答
反对 回复 2021-04-27
  • 1 回答
  • 0 关注
  • 216 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号