守护进程:守护进程会随着主进程的代码结束而结束,如果主进程结束之后还有其他子进程在运行,守护进程是不守护的,
在windows系统中开启线程需要加 if __
name__
== 'main':,Linux,mac系统中则不用
from multiprocessing import Process import time def son1(): while True: time.sleep(0.1) print('in son1') def son2(): while True: time.sleep(0.1) print('in son2') if __name__ == '__main__': s=Process(target=son1) s.daemon=True s.start() t = Process(target=son2) t.start() # t.join() #son2结束后守护进程在结束
守护线程:守护线程会随着主线程的结束而结束,如果主线程代码结束之后还有其他线程在运行,那么守护线程也守护
import time from threading import Thread def son1(): while True: print('in son1') time.sleep(1) def son2(): while True: print('in son2') time.sleep(1) s1=Thread(target=son1) s1.daemon=True s1.start() s2=Thread(target=son2) s2.start()
为什么守护进程和守护线程有区别?