生产者消费者
2018-10-03 17:58:41来源:博客园 阅读 ()
所谓,生产者与消费者模型,本质上是把进程通信的问题分开考虑
生产者,只需要往队列里面丢东西(生产者不需要关心消费者)
消费者,只需要从队列里面拿东西(消费者也不需要关心生产者)
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])
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 进程相关 2019-08-13
- python 之 并发编程(生产者消费者模型、守护进程的应用) 2019-07-24
- python学习-40 生产者和消费者模型 2019-07-24
- Python-17-反射 2019-07-24
- Python-16-继承、封装、多态 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash