# -*- coding: utf-8 -*- import time import threading from threading import Thread, Event def conn_mysql(): ‘‘‘连接数据库‘‘‘ print("(%s) start to conn_mysql" % threading.current_thread().getName()) event.wait() print("(%s conn_mysql successful)" % threading.current_thread().getName()) def check_msyql(): ‘‘‘检查mysql数据状态‘‘‘ print("(%s) checking mysql" % threading.current_thread().getName()) time.sleep(4) event.set() if __name__ == ‘__main__‘: ‘‘‘事件默认是关闭状态,执行set则为开启状态‘‘‘ event = Event() c1 = Thread(target=conn_mysql) c2 = Thread(target=conn_mysql) c3 = Thread(target=conn_mysql) c4 = Thread(target=check_msyql) c1.start() c2.start() c3.start() c4.start() # (Thread-1) start to conn_mysql # (Thread-2) start to conn_mysql # (Thread-3) start to conn_mysql # (Thread-4) checking mysql # (Thread-3 conn_mysql successful) # (Thread-1 conn_mysql successful) # (Thread-2 conn_mysql successful)
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11032620.html
时间: 2024-10-15 12:18:21