关于守护进程需要强调两点:
其一:守护进程会在主进程代码执行结束后就终止
其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic processes are not allowed to have children
from multiprocessing import Process import time def task(name): print('%s is running' %name) time.sleep(2) p=Process(target=time.sleep,args=(3,)) p.start() if __name__ == '__main__': p=Process(target=task,args=('子进程1',)) p.daemon=True p.start() p.join() print('主')
练习题
from multiprocessing import Process import time def foo(): print(123) time.sleep(1) print("end123") def bar(): print(456) time.sleep(3) print("end456") if __name__ == '__main__': p1=Process(target=foo) p2=Process(target=bar) p1.daemon=True p1.start() p2.start() print("main-------")