day41——多进程的消息队列、消息队列pipe

多进程的消息队列

消息队列指的是消息在传输过程中保存消息的容器
消息队列最经典的用法是消费者和生产者之间通过消息管道来传递消息。消费者和和生产者是不同的进程,生产者往管道中写消息,消费者从管道中读消息
 
multiprocessing模块提供了Queue类 和 Pipe函数 实现消息队列
 
1. Queue
 
用法:
In [1]: import multiprocessing

In [2]: help(multiprocessing.Queue)
Help on function Queue in module multiprocessing:

Queue(maxsize=0)
    Returns a queue object


In [3]: q = multiprocessing.Queue()         //实例化一个对象,对象的方法的用法和Queue模块中对象的方法的用法一毛一样

In [4]: q.
q.cancel_join_thread  q.empty               q.get                 q.join_thread         q.put_nowait         
q.close               q.full                q.get_nowait          q.put                 q.qsize

例子:

 1 [root@web thread_process]# cat queue4.py
 2 #!/usr/bin/env python
 3 
 4 from multiprocessing import Process, Queue
 5 def producer(q):
 6     for i in xrange(5):
 7         q.put(i)
 8         print 'put {0} into queue'.format(i)
 9 
10 def consumer(q):
11     while 1:
12         result = q.get()
13         print 'get {0} from queue'.format(result)
14         if q.empty():
15             break
16 
17 
18 if __name__ == '__main__':
19     q = Queue()
20     p = Process(target=producer, args=(q,))
21     c = Process(target=consumer, args=(q,))
22     p.start()
23     p.join()
24     c.start()
25 
26 
27 [root@web thread_process]# python queue4.py
28 put 0 into queue
29 put 1 into queue
30 put 2 into queue
31 put 3 into queue
32 put 4 into queue
33 get 0 from queue
34 get 1 from queue
35 get 2 from queue
36 get 3 from queue
37 get 4 from queue

2. Pipe

Pipe方法返回一个二元元组(conn1, conn2),两个元素分别是两个连接对象,代表管道的两端,Pipe(duplex=True) 函数有一个默认参数duplex,默认等于True,表示这个管道是全双工模式,也就是说conn1和conn2均可收发;如果duplex=False,那么conn2只负责发消息到消息队列,conn1只负责从消息队列中读取消息
 
连接对象的常用方法有三个:
  • send()   ---> 发送消息到管道
  • recv()   ---> 从管道中读取消息
  • close()   --->关闭管道
 
 
duplex=False 例子:
 1 [root@web thread_process]# cat pipe.py
 2 #!/usr/bin/env python
 3 
 4 import time
 5 from multiprocessing import Pipe, Process
 6 
 7 def producer(p):
 8     for i in xrange(5):
 9         p.send(i)
10         print 'send {0} to pipe'.format(i)
11         time.sleep(1)
12 
13 def consumer(p):
14     n = 5
15     while n>0:
16         result = p.recv()
17         print 'recv {0} from pipe'.format(result)
18         n -= 1
19 
20 if __name__ == '__main__':
21     p = Pipe(duplex=False)
22     print p
23     p1 = Process(target=producer, args=(p[1],))
24     p2 = Process(target=consumer, args=(p[0],))
25     p1.start()
26     p2.start()
27     p1.join()
28     p2.join()
29     p[0].close()
30     p[1].close()
31 
32 
33 [root@web thread_process]# python pipe.py
34 (<read-only Connection, handle 3>, <write-only Connection, handle 4>)
35 send 0 to pipe
36 recv 0 from pipe
37 send 1 to pipe
38 recv 1 from pipe
39 send 2 to pipe
40 recv 2 from pipe
41 send 3 to pipe
42 recv 3 from pipe
43 send 4 to pipe
44 recv 4 from pipe

duplex=True例子:

 1 [root@web thread_process]# cat pipe1.py
 2 #!/usr/bin/env python
 3 
 4 import time
 5 from multiprocessing import Pipe, Process
 6 
 7 def producer(p):
 8     for i in xrange(5):
 9         p.se
10         print 'send {0} to pipe'.format(i)
11         time.sleep(1)
12 
13 if __name__ == '__main__':
14     p = Pipe(duplex=True)
15     print p
16     p1 = Process(target=producer, args=(p[1],))
17     p2 = Process(target=producer, args=(p[0],))
18     p1.start()
19     p2.start()
20     p[0].close()
21     p[1].close()
22 
23 
24 [root@web thread_process]# python pipe1.py
25 (<read-write Connection, handle 5>, <read-write Connection, handle 6>)
26 send 0 to pipe
27 send 0 to pipe
28 send 1 to pipe
29 send 1 to pipe
30 send 2 to pipe
31 send 2 to pipe
32 send 3 to pipe
33 send 3 to pipe
34 send 4 to pipe
35 send 4 to pipe

 

转载于:https://www.cnblogs.com/yangjinbiao/p/8046365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分布式缓存是指数据在固定数目的集群节点间分布存储的缓存系统。它的优点是缓存容量可扩展,缺点是在扩展过程中需要大量配置并且没有容错机制。一个典型的分布式缓存系统是Memcached[1]。而Redis是另一种高性能的key-value数据库,它很好地补充了Memcached的不足,并在一些场合可以作为关系数据库的补充。Redis提供了多种客户端,使用方便,并且被广泛应用于分布式缓存中间件。 分布式消息队列是一种支持在多个节点间传递消息的系统。它可以解耦发送者和接收者,提高系统的可伸缩性和可靠性。通常,一个分布式消息队列系统需要考虑资源控制、淘汰策略、并发和分布式节点通信等方面的问题。不同的应用场景可能需要在各种特性之间权衡,例如是否需要支持缓存更新,或者假设在淘汰之前缓存是不可改变的。因此,分布式消息队列的实现可以根据不同的需求有所不同。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [分布式缓存中间件:Redis](https://blog.csdn.net/weixin_48307978/article/details/117185919)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [7天实现Go分布式缓存(day1)](https://blog.csdn.net/weixin_45750972/article/details/127854837)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值