生产者/消费者模型
生产者把产品制造出来后放入队列,
消费者从队列中取产品,
消费的速度必须小于生产的速度,否则就倒闭了。。。
代码:
#!/usr/bin/env python3 #-*- coding:utf-8 -*- from threading import Thread import time import random import queue q1 = queue.Queue(5) def consumer(num): while True: time.sleep(5) data = q1.get_nowait() print('Custom %d eating '%num + data ) def producer(worker): while True: if q1.qsize() < 2: print (" %s is Making pizza " % worker) time.sleep(1) q1.put('a pizza made by %s' %worker ) if __name__ == "__main__": worker1 = Thread(target=producer , args=('alben',)) worker2 = Thread(target=producer , args=('nichole',)) com1 = Thread(target=consumer, args=(1,)) com2 = Thread(target=consumer, args=(2,)) com3 = Thread(target=consumer, args=(3,)) worker1.start() worker2.start() com1.start() com2.start() com3.start()