多线程事件

import time
import random
from threading import Thread,Event
def conn_db(e):
    count = 0
    while count < 3:
        e.wait(1)
        if e.is_set():
            print("连接数据库成功")
            break
        else:
            count += 1
            print("第{}次连接数据库失败".format(count))
    else:
        raise TimeoutError("数据库连接超时")
def check_web(e):
    time.sleep(random.randint(1,3))
    e.set()
e = Event()
t1 = Thread(target=conn_db,args=(e,))
t2 = Thread(target=check_web,args=(e,))
t2.start()
t1.start()
模拟连接数据库
原文地址:https://www.cnblogs.com/superniao/p/10126871.html