115-python-多线程-事件
import time
import random
from threading import Thread,Event
def connect_db(e):
count = 0
while count < 3:
e.wait(0.5)
if e.is_set() == True:
print('链接数据库')
break
else:
count += 1
print('第%s次连接失败'%(count))
else:
raise TimeoutError('连接超时..')
def check_web(e):
time.sleep(random.randint(2,3))
e.set()
e = Event()
t1 = Thread(target=connect_db,args=(e,))
t2 = Thread(target=check_web,args=(e,))
t1.start()
t2.start()

浙公网安备 33010602011771号