python Event对象判断线程是否已经启动

2022-05-31  本文已影响0人  孙广宁
12.2 我们加载了一个线程,如何知道它在什么时候启动
from threading import Thread,Event
import time
def c(n,started_evt):
    print('countdown starting')
    started_evt.set()
    while n>0:
        print('T-minus',n)
        n -=1
        time.sleep(5)

started_evt =Event()

print("launching countdown")
t =Thread(target=c,args=(10,started_evt))
t.start()
started_evt.wait()
print("countdown is running")
launching countdown
countdown starting
('T-minus', 10)
countdown is running
('T-minus', 9)
('T-minus', 8)
('T-minus', 7)
('T-minus', 6)
('T-minus', 5)
('T-minus', 4)
('T-minus', 3)
('T-minus', 2)
('T-minus', 1)
上一篇下一篇

猜你喜欢

热点阅读