python使⽤dition实现⽣产者消费者
threading模块
threading.Condition() ,返回condition对象,相当于⾼级锁对象,可以使⼀个或多个线程等待直到被其他线程调度或通知
condition条件对象提供如下⽅法:
1.threading.Condition()
2.acquire(*args) 获取锁
新年画
4.wait([timeout]) 等待
⽣产者与消费者问题
ps:queue也可实现⽣产者消费者,
#⽣产者
# Consume one item
cv.acquire()
while not an_item_is_available():
cv.wait()
get_an_available_item()
刷卡门禁安装#消费者
# Produce one item
夏日美丽cv.acquire()
make_an_item_available()
⽰例:
描述:宿舍用电
⼀件产品。投放到市场上,市场需求可以看作⼀个箱⼦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__(lf,companyName,produceSpeed,info):
super(GoodsProduce,lf).__init__()
lf.produceSpeed=Decimal(2/produceSpeed).quantize(Decimal('0.00'))
lf.info=info
def run(lf):
global num
while True:
if condition.acquire():
if num < box_size:
time.sleep(lf.produceSpeed)
num += 1;
print"GoodsProduce : %s create one , now box have :%d" %(lf.companyName, num)
el:
print"NOTE: BOX is full , size %d ,filled %d" %(box_size, num)
condition.wait();
def show(lf):
print"companyName -- %s ,produceSpeed -- %s, infomation -- %s"%(lf.companyName,lf.produceSpeed,lf.info)
class GoodsConsume(threading.Thread):
def__init__(lf,cname,area,info):
super(GoodsConsume,lf).__init__()
lfame=cname
lf.area=area
lf.info=info
def run(lf):
global num
while True:
if condition.acquire():
if num >= 1:
num -= 1
print"GoodsConsumer %s get one , now box have :%d" %(lfame,num)
el:
print"NOTE: BOX is null ,plea wait ... size %d ,fillin %d" % (box_size, num)
time.sleep(1)
condition.wait();
time.sleep(1)
def show(lf):
print"GoodsConsume %s area -- %s ,infomation -- %s"%(lfame,lf.area,lf.info)
if __name__ == "__main__":
女人光身子for rver_num in range(0, 2):
rver = GoodsProduce("Prd-%d"%rver_num,rver_num+1,"this is %d prd company"%rver_num)
rver.start()
rver.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 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