python3中线程中event的小栗子
Posted 思辨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3中线程中event的小栗子相关的知识,希望对你有一定的参考价值。
import threading, time
class Boss(threading.Thread):
def run(self):
print("boss:今晚大家都要加班到22:00")
print(event.isSet())
event.set()
time.sleep(5)
print("BOSS:<22:00>可以下班了")
print(event.isSet())
event.set()
class Worker(threading.Thread):
def run(self):
event.wait()
print("worker:哎,命苦呀")
time.sleep(1)
event.clear()
event.wait()
print("worker:OH yeah!")
if __name__ == "__main__":
event = threading.Event()
threads = []
for i in range(5):
threads.append(Worker())
threads.append(Boss())
for t in threads:
t.start()
for t in threads:
t.join()
#https://www.cnblogs.com/nuomin/p/7899675.html
#https://www.cnblogs.com/zhangxinqi/p/8284687.html
运行结果:
F:\\Scrapy\\spider3\\Scripts\\python.exe F:/Django1/spider3/threading3.py
boss:今晚大家都要加班到22:00
False
worker:哎,命苦呀
worker:哎,命苦呀
worker:哎,命苦呀
worker:哎,命苦呀
worker:哎,命苦呀
BOSS:<22:00>可以下班了
False
worker:OH yeah!
worker:OH yeah!
worker:OH yeah!
worker:OH yeah!
worker:OH yeah!
Process finished with exit code 0
以上是关于python3中线程中event的小栗子的主要内容,如果未能解决你的问题,请参考以下文章
[未解决问题记录]python asyncio+aiohttp出现Exception ignored:RuntimeError('Event loop is closed')(代码片段