#生产者与消费者模式
'''
定义:在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.
该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度
案例:厨师做包子和顾客吃包子的问题。
'''
import threading
import queue,time
q = queue.Queue(maxsize=10)
#生产者
def producer(name):
count = 1
while True:
q.put('包子%d'%count)
print('生产了包子:%d'%count)
count += 1
time.sleep(1)
def consumer(name):
count = 1
while True:
print('[%s]取到了[%s],并且吃了它'%(name,q.get()))
time.sleep(1)
if __name__ == "__main__":
p = threading.Thread(target=producer,args=('张大厨',))
a = threading.Thread(target=consumer,args=('A',))
b = threading.Thread(target=consumer,args=('B',))
p.start()
a.start()
b.start()