#!/usr/bin/python
#coding:utf-8
import time
import threading
#product
product = None
#condition
con = threading.Condition()
def produce():
global product
if con.acquire():
while True:
print "商店:::正在检查商店中是否有产品...."
if product is None:
time.sleep(3)#检查中
print "商店:::哎呀,我去,没有产品了..."
print "商店:::生产中...."
time.sleep(3)
product = "我是面包!!"
print "商店:::摆放产品中..."
time.sleep(1)
print "商店:::客户们,产品现在可以买了..."
con.notify()
else:
print "商店:::还有产品"
con.wait()#收到客户的反馈,没有产品了
print "商店:::收到用户的投诉,没有产品了,赶紧给客户道歉..."
time.sleep(5) #正在给客户道歉...
def consume():
global product
if con.acquire():
while True:
print "客户: 看看有没有产品可以买..."
if product is not None:
print "客户: 哇,有产品了哦,购买中..."
product = None
print "客户: 告诉商店产品已经被买完了.."
con.notify()
else:
print "客户: 我去,还没有产品..."
print "客户: 我要多个呢,还不够赶紧给我生产!!!"
con.wait()#让客户歇会,一会就生产好了
t1 = threading.Thread(target = produce)
t1.start()
t2 = threading.Thread(target = consume)
t2.start()
python条件变量,布布扣,bubuko.com
python条件变量