所谓,生产者与消费者模型,本质上是把进程通信的问题分开考虑
生产者,只需要往队列里面丢东西(生产者不需要关心消费者)
消费者,只需要从队列里面拿东西(消费者也不需要关心生产者)
1 # 多线程实现生产者消费者模型 2 import threading 3 import random 4 import queue 5 import time 6 7 8 class Producer(threading.Thread): 9 def __init__(self, que): 10 super().__init__() 11 self.que = que 12 13 def run(self): 14 while True: 15 data = random.randint(0,100) 16 print("生产者生产了:", data) 17 self.que.put(data) 18 time.sleep(1) 19 20 21 class Consumer(threading.Thread): 22 def __init__(self, que): 23 super().__init__() 24 self.que = que 25 26 def run(self): 27 while True: 28 item = self.que.get() 29 print("消费者消费了:", item) 30 31 32 if __name__ == '__main__': 33 q = queue.Queue() 34 pro = Producer(q) 35 con = Consumer(q) 36 pro.start() 37 con.start()
1 import threading 2 import random 3 import time 4 import queue 5 6 7 def producer(que): 8 while True: 9 data = random.randint(0,100) 10 print("生产者生产了:", data) 11 que.put(data) 12 time.sleep(1) 13 14 15 def consumer(que): 16 while True: 17 item = que.get() 18 print("消费者消费了:", item) 19 20 21 if __name__ == '__main__': 22 q = queue.Queue() 23 pro_td = threading.Thread(target=producer, args=(q, )) 24 pro_td.start() 25 con_td = threading.Thread(target=consumer, args=(q, )) 26 con_td.start()
1 import multiprocessing 2 import random 3 import time 4 5 6 def producer(que): 7 while True: 8 data = random.randint(0,100) 9 print("生产者生产了:", data) 10 que.put(data) 11 time.sleep(1) 12 13 14 def consumer(que): 15 while True: 16 item = que.get() 17 print("消费者消费了:", item) 18 19 20 if __name__ == '__main__': 21 q = multiprocessing.Manager().Queue() 22 pro_td = multiprocessing.Process(target=producer, args=(q, )) 23 pro_td.start() 24 con_td = multiprocessing.Process(target=consumer, args=(q, )) 25 con_td.start() 26 pro_td.join()
1 import multiprocessing 2 import random 3 import time 4 5 6 def producer(que): 7 while True: 8 data = random.randint(0,100) 9 print("生产者生产了:", data) 10 que.put(data) 11 time.sleep(1) 12 13 14 def consumer(que): 15 while True: 16 item = que.get() 17 print("消费者消费了:", item) 18 19 20 if __name__ == '__main__': 21 q = multiprocessing.Manager().Queue() 22 pro_td = multiprocessing.Process(target=producer, args=(q, )) 23 pro_td.start() 24 con_td = multiprocessing.Process(target=consumer, args=(q, )) 25 con_td.start() 26 pro_td.join()
1 # gevent 协程实现生产者消费者 2 import gevent 3 from gevent import monkey; monkey.patch_all() 4 import random 5 from gevent.queue import Queue 6 import time 7 8 9 def producer(que): 10 while True: 11 data = random.randint(0, 100) 12 print("生产者生产了:", data) 13 q.put(data) 14 time.sleep(1) 15 16 17 def consumer(que): 18 while True: 19 item = que.get() 20 print("消费者消费了:", item) 21 22 23 if __name__ == '__main__': 24 q = Queue() 25 pro = gevent.spawn(producer, q) 26 con = gevent.spawn(consumer, q) 27 gevent.joinall([pro, con])