生产者消费者

2018-10-03 17:58:41来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

1、概念

所谓,生产者与消费者模型,本质上是把进程通信的问题分开考虑

生产者,只需要往队列里面丢东西(生产者不需要关心消费者)

消费者,只需要从队列里面拿东西(消费者也不需要关心生产者)

 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])
gevent实现生产者消费者

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:urllib爬虫(流程+案例)

下一篇:Microbit MicroPython 介绍