1. 程式人生 > >Python 多線程的程序不結束多進程的程序不結束的區別

Python 多線程的程序不結束多進程的程序不結束的區別

__main__ true spa code 結束 class 代碼執行 reading def

import time
from threading import Thread
from multiprocessing import Process

#守護進程:主進程代碼執行運行結束,守護進程隨之結束

#守護線程:守護線程會等待所有非守護線程運行結束才結束

def f1():
    time.sleep(2)
    print(1號線程)

def f2():
    time.sleep(3)
    print(2號線程)
if __name__ == __main__:
    # t1 = Thread(target=f1,)
    # t2 = Thread(target=f2,)
# t1.daemon = True # t2.daemon = True # t1.start() # t2.start() # print(‘主線程結束‘) t1 = Process(target=f1, ) t2 = Process(target=f2, ) # t1.daemon = True # # t2.daemon = True t1.start() t2.start() print(主進程結束)

Python 多線程的程序不結束多進程的程序不結束的區別