import time # 一,要在線程中執行的耗時函數 def countdown(n): while n>0: print("t-minus",n) n -=1 time.sleep(5) # create and launch a thread from threading import Thread # 二,這里啟動線程,在線程中執行 countdown函數,主線程繼續往下走 t=Thread(target=countdown,args=(10,)) # t=Thread(target=countdown,args=(10,),daemon=True) t.start() # 三,等線程t完成后,結束整個任務,主進程會在這里阻塞 t.join() # 四,判斷線程是否已經銷毀 if t.is_alive(): print("still running") else: print("completed")
