python3 线程事件Event

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值