Python多线程之event
来源:互联网 发布:淘宝有哪些女装潮店 编辑:程序博客网 时间:2024/05/19 12:27
写在前面的话
看了网上一些大牛相关博客,不是很理解,内核对象啊什么的。这篇博客用白话记录自己的学习过程以及一些理解。
event精粹
概述
事件(event)用于线程间同步和通信。比如线程A要完成某一任务(event)线程B才能执行后面的代码,怎么实现呢,就是用event。
注意:
wait方法是阻塞的
设置完set后要使用clear方法,否则wait为真,不会等待
上代码
#!/usr/bin python#coding=utf-8import threadingimport timedef producer(): print u'等人来买包子....' event.wait() #event.clear() print event.isSet() print u'chef:sb is coming for baozi...' print u'chef:making a baozi for sb...' time.sleep(5) print u'chef:你的包子好了...' event.set() def consumer(): print u'chenchao:去买包子....' event.set() time.sleep(2) print 'chenchao:waiting for baozi to be ready...' print event.wait() print u'chenchao:哎呀真好吃....'event = threading.Event()p = threading.Thread(target=producer,args=())c = threading.Thread(target=consumer,args=())p.start()c.start()
clear()注释掉的结果
等人来买包子....chenchao:去买包子....Truechef:sb is coming for baozi...chef:making a baozi for sb...chenchao:waiting for baozi to be ready...Truechenchao:哎呀真好吃....chef:你的包子好了...
最后两行显然有问题,包子还没好就可以print了,要使用clear
方法清除设置的set状态。
#!/usr/bin python#coding=utf-8import threadingimport timedef producer(): print u'等人来买包子....' event.wait() event.clear() print event.isSet() print u'chef:sb is coming for baozi...' print u'chef:making a baozi for sb...' time.sleep(5) print u'chef:你的包子好了...' event.set() def consumer(): print u'chenchao:去买包子....' event.set() time.sleep(2) print 'chenchao:waiting for baozi to be ready...' print event.wait() print u'chenchao:哎呀真好吃....'event = threading.Event()p = threading.Thread(target=producer,args=())c = threading.Thread(target=consumer,args=())p.start()c.start()
结果是这样的
等人来买包子....chenchao:去买包子....Falsechef:sb is coming for baozi...chef:making a baozi for sb...chenchao:waiting for baozi to be ready...chef:你的包子好了...Truechenchao:哎呀真好吃....
模拟异步模型中的select
#!/usr/bin python#coding=utf-8import threadingimport timedef producer(): print u'等人来买包子....' event.wait() event.clear() print event.isSet() print u'chef:sb is coming for baozi...' print u'chef:making a baozi for sb...' time.sleep(5) print u'chef:你的包子好了...' event.set() def consumer(): print u'chenchao:去买包子....' event.set() time.sleep(2) print 'chenchao:waiting for baozi to be ready...' while True: if event.isSet(): print u'真好吃...' break else: print u'好慢啊...' time.sleep(1)#相当于去做自己的事去了event = threading.Event()p = threading.Thread(target=producer,args=())c = threading.Thread(target=consumer,args=())p.start()c.start()
结果
等人来买包子....chenchao:去买包子....Falsechef:sb is coming for baozi...chef:making a baozi for sb...chenchao:waiting for baozi to be ready...好慢啊...好慢啊...好慢啊...好慢啊...chef:你的包子好了...真好吃...
阅读全文
0 0
- Python多线程之event
- Python多线程之threading Event
- python 多线程之信号机Semaphore、事件Event
- Python 多线程 Event
- 多线程Event之详解
- 多线程之Event
- python event多线程回调
- day11_python多线程之event代码
- 【Python】threading.Event模块控制多线程
- python线程通信之event
- Python线程之threading.Event
- Windows中多线程同步之事件(Event)
- 多线程同步之Event(事件对象)
- Win32多线程之事件(Event Objects)
- C++多线程同步之事件(Event)
- Event多线程
- 多线程---event
- 多线程Event
- 理解ROS话题(六)
- Combination and Permutation
- Linux 内核剖析
- Android性能优化之避免ANR
- leofs manager(三)
- Python多线程之event
- spring是用aop:before报错解决方法
- [Leetcode] 168. Excel Sheet Column Title 解题报告
- tcpdump使用与Wireshark抓包分析
- Server
- Nim游戏
- linux 学习文档(二)
- LeetCode 609: Find Duplicate File in System(python)
- 在c++中使用指向数组的引用