python使用threading.condition实现生产者消费者
2017-05-29 01:44
726 查看
threading模块
threading.Condition() ,返回condition对象,相当于高级锁对象,可以使一个或多个线程等待直到被其他线程调度或通知
condition条件对象提供如下方法:
1.threading.Condition()
2.acquire(*args) 获取锁
3.release()释放锁
4.wait([timeout]) 等待
5.notify(n=1) / notifyAll() 唤醒
生产者与消费者问题
ps:queue也可实现生产者消费者,<<详情参考>>
示例:
描述:
一件产品。投放到市场上,市场需求可以看作一个箱子box,有一个容量限度
当小于box大小,生产者可以生产,否则等待消费者消费,生产者有生产效率,使用speed表示
当产品大于0,才可进行消费,否则就等待生产者生产
结果:
threading.Condition() ,返回condition对象,相当于高级锁对象,可以使一个或多个线程等待直到被其他线程调度或通知
condition条件对象提供如下方法:
1.threading.Condition()
2.acquire(*args) 获取锁
3.release()释放锁
4.wait([timeout]) 等待
5.notify(n=1) / notifyAll() 唤醒
生产者与消费者问题
ps:queue也可实现生产者消费者,<<详情参考>>
#生产者 # Consume one item cv.acquire() while not an_item_is_available(): cv.wait() get_an_available_item() cv.release()
#消费者 # Produce one item cv.acquire() make_an_item_available() cv.notify() cv.release()
示例:
描述:
一件产品。投放到市场上,市场需求可以看作一个箱子box,有一个容量限度
当小于box大小,生产者可以生产,否则等待消费者消费,生产者有生产效率,使用speed表示
当产品大于0,才可进行消费,否则就等待生产者生产
# encoding=utf8 import threading import time from decimal import Decimal condition = threading.Condition() num = 0 box_size=15 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): global num while True: if condition.acquire(): if num < box_size: time.sleep(self.produceSpeed) num += 1; print "GoodsProduce : %s create one , now box have :%d" %(self.companyName, num) condition.notify() condition.release() else: print "NOTE: BOX is full , size %d ,filled %d" %(box_size, num) condition.wait(); 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): global num while True: if condition.acquire(): if num >= 1: num -= 1 print "GoodsConsumer %s get one , now box have :%d" %(self.cname,num) condition.notify() condition.release() else: print "NOTE: BOX is null ,please wait ... size %d ,fillin %d" % (box_size, num) time.sleep(1) condition.wait(); time.sleep(1) 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 company companyName -- Prd-1 ,produceSpeed -- 1.00, infomation -- this is 1 prd company GoodsConsume cus-0 area -- area-0 ,infomation -- this is 0 customer GoodsConsume cus-1 area -- area-1 ,infomation -- this is 1 customer GoodsConsume cus-2 area -- area-2 ,infomation -- this is 2 customer GoodsConsume cus-3 area -- area-3 ,infomation -- this is 3 customer GoodsConsume cus-4 area -- area-4 ,infomation -- this is 4 customer GoodsProduce : Prd-0 4000 create one , now box have :1 GoodsProduce : Prd-0 create one , now box have :2 GoodsProduce : Prd-0 create one , now box have :15 NOTE: BOX is full , size 15 ,filled 15 GoodsConsumer cus-1 get one , now box have :14 GoodsConsumer cus-2 get one , now box have :13 GoodsConsumer cus-3 get one , now box have :12 GoodsConsumer cus-4 get one , now box have :11 GoodsProduce : Prd-1 create one , now box have :12 GoodsProduce : Prd-1 create one , now box have :13 GoodsProduce : Prd-1 create one , now box have :14 GoodsProduce : Prd-1 create one , now box have :15 NOTE: BOX is full , size 15 ,filled 15 GoodsConsumer cus-2 get one , now box have :14 GoodsConsumer cus-4 get one , now box have :13 GoodsConsumer cus-0 get one , now box have :12 GoodsProduce : Prd-0 create one , now box have :13 GoodsProduce : Prd-0 create one , now box have :14 GoodsProduce : Prd-0 create one , now box have :15 NOTE: BOX is full , size 15 ,filled 15 GoodsConsumer cus-1 get one , now box have :14 GoodsConsumer cus-3 get one , now box have :13 GoodsProduce : Prd-1 create one , now box have :14 GoodsProduce : Prd-1 create one , now box have :15 NOTE: BOX is full , size 15 ,filled 15
相关文章推荐
- 在python中实现生产者和消费者的例子(五):使用threading模块和全局变量
- python使用队列实现生产者消费者
- 使用lock&condition实现生产者消费者
- 使用Lock和Condition实现生产者和消费者
- python使用queue队列实现生产者消费者
- python中线程的使用以及生产者和消费者的实现
- 使用 Lock 与Condition 实现生产者消费者
- 在python中实现生产者和消费者的例子(一):使用multiprocessing和pipe()
- Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]
- 使用Lock和Condition实现生产者消费者模型
- 在python中实现生产者和消费者的例子(四):使用thread模块和全局变量
- 使用Python实现生产者消费者问题
- 使用Lock的Condition实现生产者消费者
- 使用JUC并发工具包的Lock和Condition,实现生产者和消费者问题中的有界缓存
- 使用ReentrantLock和Condition实现生产者消费者模型
- 使用Python实现生产者消费者问题
- 在python中实现生产者和消费者的例子(二):使用multiprocessing和queue
- Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]
- 在python中实现生产者和消费者的例子(三):使用fork和pipe
- Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]