python3 线程事件Event
Posted lilyxiaoyy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
以上是关于python3 线程事件Event的主要内容,如果未能解决你的问题,请参考以下文章
[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段