python中並發----線程的啟動和停止


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")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM