code
macname@MacdeMBP Desktop % macname@MacdeMBP Desktop % cat test.py import time from multiprocessing import Process def func(name, sec): print(name, 123) time.sleep(sec) print(name, "123end") if __name__ == '__main__': p1 = Process(target=func, args=("进程1", 1)) p2 = Process(target=func, args=("进程2", 2)) p3 = Process(target=func, args=("进程3", 3)) p1.daemon = True # 必须在start()之前设置守护进程 p1.start() p2.start() p3.start() time.sleep(0.5) print("主进程结束,主进程还得等待其他子进程(非守护进程)结束才行") macname@MacdeMBP Desktop % python3 test.py 进程1 123 进程2 123 进程3 123 主进程结束,主进程还得等待其他子进程(非守护进程)结束才行 进程2 123end 进程3 123end macname@MacdeMBP Desktop %