python 生产者消费者模型
import threading import queue import time lock = threading.RLock() q = queue.Queue() #创建一个队列 def producer(id): ''' 生产者模型 :param id: :return: ''' print('厨师%s开始做包子'% id) for i in range(1,11): q.put('厨师%s做的第%s个包子'%(id,i)) time.sleep(1) print('厨师%s包子做完了' % id) for i in range(1,4): t = threading.Thread(target=producer, args=(i,)) t.start() def consumer(id): ''' 消费者模型 :param id: :return: ''' while True: try: baozi2 = q.get(timeout=5) print(baozi2, '被用户%s吃了' % id) except Exception as e: print('用户%s吃完包子了'% id) break for j in range(1,3): t = threading.Thread(target=consumer, args=(j,)) t.start()