- 主進程或等待子進程執行完
# 輸出over后主進程內容已經執行完了,但是會等待子進程執行完
from multiprocessing import *
from time import *
def print_info():
for i in range(10):
print(i)
sleep(0.2)
if __name__ == '__main__':
p = Process(target=print_info)
p.start()
sleep(0.5)
print('over')
- 主進程強制子進程結束或直接結束程序
from multiprocessing import *
from time import *
def print_info():
for i in range(10):
print(i)
sleep(0.2)
if __name__ == '__main__':
p = Process(target=print_info)
# 把子進程設置為守護進程,主進程結束時子進程直接銷毀
# p.daemon = True
p.start()
sleep(0.5)
# 退出主程序前先讓子進程銷毀
p.terminate()
print('over')