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:Scrapyspider3Scriptspython.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

原文地址:https://www.cnblogs.com/qingsheng/p/9617265.html