from queue import Queue from threading import Thread import time import random class Producer(Thread): def __init__(self,name,queue): Thread.__init__(self,name=name) self.data=queue def run(self): for i in range(5): print('生产者%s将产品%d加入队列'%(self.getName(),i)) self.data.put(i) time.sleep(random.random()) print('生产者%s完成'%self.getName()) class Consumer(Thread): def __init__(self, name, queue): Thread.__init__(self, name=name) self.data = queue def run(self): for i in range(5): val = self.data.get(i) print('消费者%s将产品%d从队列取出' % (self.getName(), val)) time.sleep(random.random()) print('消费者%s完成' % self.getName()) if __name__ == '__main__': print('主线程开始') queue = Queue() producer = Producer('Producer',queue) consumer = Consumer('Consumer',queue) producer.start() consumer.start() producer.join() consumer.join() print('主线程结束')
python-生产者消费者模式
最新推荐文章于 2024-05-23 11:14:07 发布