python使用queue队列实现生产者消费者
来源:互联网 发布:java 线程池 添加任务 编辑:程序博客网 时间:2024/06/06 11:44
queue使用方法,<<详情参考>>
使用queue队列实现生产者消费者问题
另threading.condition也可实现生产者消费者问题,<<详情参考>>
示例:
生产者可以有多个,消费者可以有多个,但是市场容量是有一定限度的
一件产品。投放到市场上,市场需求可以看作一个箱子box,是容量限度
当小于box大小,即队列没有满full,生产者可以生产,否则等待消费者消费,生产者有生产效率,使用speed表示
当产品大于0,即队列不为空null,才可进行消费,否则就等待生产者生产
import threadingimport queueimport timeimport randomfrom decimal import Decimalbox=15queuelist = queue.Queue(box)class GoodsProduce(threading.Thread): def __init__(self,companyName,produceSpeed,info): super(GoodsProduce,self).__init__() self.companyName=companyName self.produceSpeed=Decimal(2/produceSpeed).quantize(Decimal('0.00')) self.info=info def run(self): while True: if not queuelist.full(): time.sleep(self.produceSpeed) str="goods-%d"%random.randint(1,100) queuelist.put(str) print "GoodsProduce : %s create %s , now box have :%d" % (self.companyName,str, queuelist.qsize()) else: print "NOTE: BOX is full , size %d ,filled %d" % (box, box) time.sleep(1) def show(self): print "companyName -- %s ,produceSpeed -- %s, infomation -- %s"%(self.companyName,self.produceSpeed,self.info)class GoodsConsume(threading.Thread): def __init__(self,cname,area,info): super(GoodsConsume,self).__init__() self.cname=cname self.area=area self.info=info def run(self): while True: if not queuelist.empty(): gname=queuelist.get() print "GoodsConsumer %s get %s , now box have :%d" % (self.cname,gname, queuelist.qsize()) else : time.sleep(1) if queuelist.empty(): print "NOTE: BOX is null ,please wait ... size %d ,fillin 0" % (box) time.sleep(2) def show(self): print "GoodsConsume %s area -- %s ,infomation -- %s"%(self.cname,self.area,self.info)if __name__ == "__main__": for server_num in range(0, 2): server = GoodsProduce("Prd-%d"%server_num,server_num+1,"this is %d prd company"%server_num) server.start() server.show() for customer_num in range(0, 5): customer = GoodsConsume("cus-%d"%customer_num,"area-%d"%customer_num,"this is %d customer"%customer_num) customer.start() customer.show()
输出:
companyName -- Prd-0 ,produceSpeed -- 2.00, infomation -- this is 0 prd companycompanyName -- Prd-1 ,produceSpeed -- 1.00, infomation -- this is 1 prd companyGoodsConsume cus-0 area -- area-0 ,infomation -- this is 0 customerGoodsConsume cus-1 area -- area-1 ,infomation -- this is 1 customerGoodsConsume cus-2 area -- area-2 ,infomation -- this is 2 customerGoodsConsume cus-3 area -- area-3 ,infomation -- this is 3 customerGoodsConsume cus-4 area -- area-4 ,infomation -- this is 4 customerGoodsProduce : Prd-1 create goods-16 , now box have :1GoodsProduce : Prd-0 create goods-9 , now box have :2GoodsProduce : Prd-1 create goods-44 , now box have :3GoodsConsumer cus-1 get goods-16 , now box have :2GoodsConsumer cus-2 get goods-9 , now box have :1GoodsConsumer cus-0 get goods-44 , now box have :0GoodsProduce : Prd-1 create goods-41 , now box have :1GoodsConsumer cus-3 get goods-41 , now box have :0GoodsProduce : Prd-0 create goods-61 , now box have :1GoodsProduce : Prd-1 create goods-71 , now box have :2GoodsConsumer cus-1 get goods-61 , now box have :1GoodsConsumer cus-2 get goods-71 , now box have :0NOTE: BOX is null ,please wait ... size 15 ,fillin 0
阅读全文
0 0
- python使用queue队列实现生产者消费者
- python:多线程+队列Queue 实现:生产者和消费者demo
- python使用队列实现生产者消费者
- Python 多线程|Queue队列|生产者消费者模式|
- Python 生产者消费者--队列实现
- 使用阻塞队列BlockingQueue实现生产者消费者
- 使用AutoResetEvent信号实现生产者消费者队列
- 在python中实现生产者和消费者的例子(二):使用multiprocessing和queue
- python使用threading.condition实现生产者消费者
- Python使用生成器实现生产者消费者模式
- 多进程 队列 实现生产者消费者模型 python 笔记
- 生产者消费者JAVA队列实现
- 实现一个生产者消费者队列
- 队列实现生产者消费者模型
- 阻塞队列实现生产者消费者
- java使用阻塞队列实现生产者消费者模式
- 生产者、消费者问题,python实现
- 使用Python多线程如何实现生产者消费者模式?
- mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)
- 5月29号
- LR(0) 文法分析器
- ubuntn 系统中安装pycharm ,注册码和设置搜狗输入法
- 第八届蓝桥杯国赛C/C++ B组试题
- python使用queue队列实现生产者消费者
- 读写锁代码编写
- 搜索 F题
- C语言程序设计(31)
- bzoj1874 取石子游戏
- java 最全的类型转换
- 机器学习,模型评估(udacity nanodegree 1-3总结)
- 关于<input type="button" value="搜索">默认样式的清除
- React Native学习笔记(1)--九宫格实现