我正在尝试理解多处理模块。以下是我的代码。
from multiprocessing import Process, current_process
#from time import time
import time
def work(delay):
p = current_process()
print p.name, p.pid, p.deamon
time.sleep(delay)
print 'Finised deamon work'
def main():
print 'Starting Main Process'
p = Process(target=work, args=(2,))
p.deamon = True
p.start()
print 'Exiting Main Process'
if __name__ == '__main__':
main()
输出:
Starting Main Process
Exiting Main Process
Process-1 7863 True
Finised deamon work
我希望主进程在deamon进程之前退出(睡眠2秒)。由于主进程退出,deamon进程也应该退出。但输出让我感到困惑。
预期产出:
Starting Main Process
Exiting Main Process
Process-1 7863 True
我对多处理模块的理解是错误的吗?