码迷,mamicode.com
首页 > 编程语言 > 详细

python3中线程中event的小栗子

时间:2018-09-10 11:08:01      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:script   imp   run   nis   ide   print   log   main   结果   

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的小栗子

标签:script   imp   run   nis   ide   print   log   main   结果   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!