线程 事件

时间:2020-09-17 17:01:57   收藏:0   阅读:26
import time
import random
from threading import Thread,Event

def connect_db(e):
count = 0
while count<3:
e.wait(1) #状态为false的时候,我只等待1s就结束
if e.is_set() == True:
print(‘连接数据库成功‘)
break
else:
count+=1
print(‘第%s次连接失败‘%count)
else:
raise TimeoutError(‘连接数据库超时‘)



def check_web(e):
time.sleep(random.randint(0, 3))
e.set()

e=Event()
t1 = Thread(target = connect_db,args=(e,))
t2 = Thread(target = check_web,args=(e,))
t1.start()
t2.start()

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!