# -*- 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)