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")
原文地址:https://www.cnblogs.com/chaojiyingxiong/p/13926144.html