day41——多进程的消息队列、消息队列pipe
生活随笔
收集整理的這篇文章主要介紹了
day41——多进程的消息队列、消息队列pipe
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多進程的消息隊列
消息隊列指的是消息在傳輸過程中保存消息的容器 消息隊列最經典的用法是消費者和生產者之間通過消息管道來傳遞消息。消費者和和生產者是不同的進程,生產者往管道中寫消息,消費者從管道中讀消息 multiprocessing模塊提供了Queue類 和 Pipe函數 實現消息隊列 1. Queue 用法: In [1]: import multiprocessingIn [2]: help(multiprocessing.Queue) Help on function Queue in module multiprocessing:Queue(maxsize=0)Returns a queue objectIn [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 queue2. Pipe
Pipe方法返回一個二元元組(conn1, conn2),兩個元素分別是兩個連接對象,代表管道的兩端,Pipe(duplex=True) 函數有一個默認參數duplex,默認等于True,表示這個管道是全雙工模式,也就是說conn1和conn2均可收發(fā);如果duplex=False,那么conn2只負責發(fā)消息到消息隊列,conn1只負責從消息隊列中讀取消息 連接對象的常用方法有三個:- send() ? ---> 發(fā)送消息到管道
- recv() ? ---> 從管道中讀取消息
- close() ? --->關閉管道
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
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的day41——多进程的消息队列、消息队列pipe的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LDAP服务器
- 下一篇: Eclipse 运行程序