Python 生产者和消费者模型
2016-05-05 22:51
645 查看
import time def consumer(name): print ("%s consumer one product" %name) while True: baozi = yield print ("baozi[%s] bei [%s] chile" %(baozi,name)) def producter(name): c = consumer('A') c2 = consumer('B') c.__next__() c2.__next__() print ("begin to make baozi") for i in range(10): time.sleep(1) print ("make two baozi") c.send(i) c2.send(i) producter('alex') 结果:
A consumer one product
B consumer one product
begin to make baozi
make two baozi
baozi[0] bei [A] chile
baozi[0] bei [B] chile
make two baozi
baozi[1] bei [A] chile
baozi[1] bei [B] chile
make two baozi
baozi[2] bei [A] chile
baozi[2] bei [B] chile
make two baozi
baozi[3] bei [A] chile
baozi[3] bei [B] chile
make two baozi
baozi[4] bei [A] chile
baozi[4] bei [B] chile
make two baozi
baozi[5] bei [A] chile
baozi[5] bei [B] chile
make two baozi
baozi[6] bei [A] chile
baozi[6] bei [B] chile
make two baozi
baozi[7] bei [A] chile
baozi[7] bei [B] chile
make two baozi
baozi[8] bei [A] chile
baozi[8] bei [B] chile
make two baozi
baozi[9] bei [A] chile
baozi[9] bei [B] chile
结论:c,c2返回为迭代器,故而consumer为生产者,注意用断点调试的方法来查看。
相关文章推荐
- 《Web接口开发与自动化测试 -- 基于Python语言》---现已出版。
- python 类中的__nozero__ ,__len__ 重写,可以定义对象的布尔值是True or False
- 用图形画出caffe输出数据的python程序&&git基础命令
- python 模拟鼠标键盘输入PyUserInput
- 字符串编码与Python 3编码
- 【Python工具】Excle 读写 (使用 xlrd 和 xlrt)
- 【Python】将Python发布成exe
- python自动化脚本ftp应用
- 设置python 模块调用路径
- python基础教程 re模块 学习笔记
- OpenCV-Python教程(9)(10)(11): 使用霍夫变换检测直线 直方图均衡化 轮廓检测
- 整理-python常用模块使用说明
- OpenCV-Python教程(6)(7)(8): Sobel算子 Laplacian算子 Canny边缘检测
- 获取列表中多个相同元素的索引值
- OpenCV Python教程(3)(4)(5): 直方图的计算与显示 形态学处理 初级滤波内
- python excel2007 折行显示
- python re match 与 findall 实际操作上的差异
- OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并
- python is 同一性运算符
- python PIL图像操作