day41——多进程的消息队列、消息队列pipe
生活随笔
收集整理的這篇文章主要介紹了
day41——多进程的消息队列、消息队列pipe
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
多進(jìn)程的消息隊(duì)列
消息隊(duì)列指的是消息在傳輸過程中保存消息的容器 消息隊(duì)列最經(jīng)典的用法是消費(fèi)者和生產(chǎn)者之間通過消息管道來傳遞消息。消費(fèi)者和和生產(chǎn)者是不同的進(jìn)程,生產(chǎn)者往管道中寫消息,消費(fèi)者從管道中讀消息 multiprocessing模塊提供了Queue類 和 Pipe函數(shù) 實(shí)現(xiàn)消息隊(duì)列 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() //實(shí)例化一個(gè)對(duì)象,對(duì)象的方法的用法和Queue模塊中對(duì)象的方法的用法一毛一樣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方法返回一個(gè)二元元組(conn1, conn2),兩個(gè)元素分別是兩個(gè)連接對(duì)象,代表管道的兩端,Pipe(duplex=True) 函數(shù)有一個(gè)默認(rèn)參數(shù)duplex,默認(rèn)等于True,表示這個(gè)管道是全雙工模式,也就是說conn1和conn2均可收發(fā);如果duplex=False,那么conn2只負(fù)責(zé)發(fā)消息到消息隊(duì)列,conn1只負(fù)責(zé)從消息隊(duì)列中讀取消息 連接對(duì)象的常用方法有三個(gè):- send() ? ---> 發(fā)送消息到管道
- recv() ? ---> 從管道中讀取消息
- close() ? --->關(guān)閉管道
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?
轉(zhuǎn)載于:https://www.cnblogs.com/yangjinbiao/p/8046365.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的day41——多进程的消息队列、消息队列pipe的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LDAP服务器
- 下一篇: Eclipse 运行程序