1. 程式人生 > >Python生產者和消費者

Python生產者和消費者

sta produce time 並且 get max 消費者 += .get

# Author:XiangLiang
import threading,time
import queue

q = queue.Queue(maxsize=10)
def Producer(name):
count = 1
while True:
q.put("骨頭 %s" %count)
print("生產了骨頭",count)
count += 1
time.sleep(0.1)

def Consumer(name):
while True:
print("%s 取到 %s 並且吃了" %(name,q.get()))
time.sleep(1)

p = threading.Thread(target=Producer,args=("ZS",))
c = threading.Thread(target=Consumer,args=("XiaoHe",))
c1 = threading.Thread(target=Consumer,args=("HuZi",))

p.start()
c.start()
c1.start()

Python生產者和消費者