Python实现协程的生产者与消费者
2016-05-16 17:25
573 查看
#-*- coding:utf-8 -*- __author__ = '杨鑫' ''' 生产者与消费者对于资源的读取问题: 此处写法为利用协程的思想实现,利用yield生成器, 生产者生产一个东西,接着调到yield的下一条语句, 实现消费者,消费后,又返回生产者处,继续生产, 接着道消费者处消费反复反复...... ''' def consumer(): #一个生成器 r = '' while True: n = yield r if not n: return print ('[CONSUMER] Consuming %s...'%n) r = '200 OK' def produce(c): c.send(None) #启动生成器 n = 0 while n < 5: n += 1 print('[PRODUCER] Producing %s..'%n) r = c.send(n) print('[PRODUCER] Consumer: %s'% r) c.close() c = consumer() produce(c) #传递一个生成器
相关文章推荐
- 机器学习实战--k近邻算法
- python实现欧拉计划24题
- 灰帽子Python 学习记录 6
- Python-Jenkins API使用 —— 在后端代码中操控Jenkins
- windows下用Python把pdf文件转化为图片(png格式)
- [python爬虫]模拟登陆扇贝单词
- PySide——Python图形化界面入门教程(二)
- 自学Python实现简答的爬虫
- Element is not clickable at point (1108, 190). Other element would receive the click
- Python unittest测试框架详解
- PYTHON介绍
- python数据分析2:双色球 蓝红球分析统计
- Python中的反射
- 安装PIL库时提示python未注册错误(自定义python安装路径)
- 如何进行 Python性能分析,你才能如鱼得水?
- [Python]第六讲:tuple
- [Python]第五讲:list
- 如何进行 Python性能分析,你才能如鱼得水?
- Python-open读写模式
- python操作字典类型的常用方法(推荐)