程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> python的multiprocessing多進程通信的pipe和queue介紹

python的multiprocessing多進程通信的pipe和queue介紹

編輯:關於PHP編程
    python的multiprocessing提供了IPC(Pipe和Queue),使Python多進程並發,效率上更高。本文我們就來詳細介紹一下pipe和queue。    

    這兩天溫故了python的multiprocessing多進程模塊,看到的pipe和queue這兩種ipc方式,啥事ipc? ipc就是進程間的通信模式,常用的一半是socke,rpc,pipe和消息隊列等。


    今個就再把pipe和queue搞搞。

    代碼如下   #coding:utf-8
    import multiprocessing
    import time

    def proc1(pipe):
    while True:
    for i in xrange(10000):
    print "發送 %s"%i
    pipe.send(i)
    time.sleep(1)

    def proc2(pipe):
    while True:
    print 'proc2 接收:',pipe.recv()
    time.sleep(1)

    def proc3(pipe):
    while True:
    print 'proc3 接收:',pipe.recv()
    time.sleep(1)
    # Build a pipe
    pipe = multiprocessing.Pipe()
    print pipe

    # Pass an end of the pipe to process 1
    p1 = multiprocessing.Process(target=proc1, args=(pipe[0],))
    # Pass the other end of the pipe to process 2
    p2 = multiprocessing.Process(target=proc2, args=(pipe[1],))


    p1.start()
    p2.start()
    p1.join()
    p2.join()

    python的multiprocessing多進程通信的pipe和queue介紹   三聯


    不只是multiprocessing的pipe,包括其他的pipe實現,都只是兩個進程之間的游玩,我給你,你來接收 或者是你來,我接收。 當然也可以做成雙工的狀態。

    queue的話,可以有更多的進程參與進來。用法和一些別的queue差不多。


    看下官網的文檔:

    multiprocessing.Pipe([duplex])

    Returns a pair (conn1, conn2) of Connection objects representing the ends of a pipe.

    #兩個pipe對象。用這兩個對象,來互相的交流。


    If duplex is True (the default) then the pipe is bidirectional. If duplex is False then the pipe is unidirectional: conn1 can only be used for receiving messages and conn2 can only be used for sending messages.


    class multiprocessing.Queue([maxsize])

    Returns a process shared queue implemented using a pipe and a few locks/semaphores. When a process first puts an item on the queue a feeder thread is started which transfers objects from a buffer into the pipe.

    #隊列的最大數


    The usual Queue.Empty and Queue.Full exceptions from the standard library’s Queue module are raised to signal timeouts.


    Queue implements all the methods of Queue.Queue except for task_done() and join().


    qsize()

    Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable.

    #隊列的大小


    Note that this may raise NotImplementedError on Unix platforms like Mac OS X where sem_getvalue() is not implemented.


    empty()

    Return True if the queue is empty, False otherwise. Because of multithreading/multiprocessing semantics, this is not reliable.

    #是否孔了。 如果是空的,他回返回一個True 的狀態。


    full()

    Return True if the queue is full, False otherwise. Because of multithreading/multiprocessing semantics, this is not reliable.

    #隊列的狀態是否滿了。


    put(obj[, block[, timeout]])

    Put obj into the queue. If the optional argument block is True (the default) and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Queue.Full exception if no free slot was available within that time. Otherwise (block is False), put an item on the queue if a free slot is immediately available, else raise the Queue.Full exception (timeout is ignored in that case).

    #塞入隊列,可以加超時的時間。

    put_nowait(obj)

    Equivalent to put(obj, False).

    #這裡是不堵塞的


    get([block[, timeout]])

    Remove and return an item from the queue. If optional args block is True (the default) and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Queue.Empty exception if no item was available within that time. Otherwise (block is False), return an item if one is immediately available, else raise the Queue.Empty exception (timeout is ignored in that case).

    #獲取狀態


    get_nowait()

    Equivalent to get(False).

    #不堵塞的get隊列裡面的數據


    Queue has a few additional methods not found in Queue.Queue. These methods are usually unnecessary for most code:


    close()

    Indicate that no more data will be put on this queue by the current process. The background thread will quit once it has flushed all buffered data to the pipe. This is called automatically when the queue is garbage collected.

    #關閉,省當前進程的資源。



    我配置了multiprocessing隊裡長度是3個,然後當我放入的是第四個的時候, 會發現一只的堵塞,他是在等待,有人把數據get掉一個,那個時候 他才能繼續的塞入 。如果用put_nowait()的話,隊列超出會立馬會一個error的。


    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.pyc in put_nowait(self, obj)


    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.pyc in put(self, obj, block, timeout)

    wKioL1QMjmuR30vjAAOEnmz0ElE220.jpg


    下面是一段測試的代碼,同學們可以跑跑demo,感受下。

    代碼如下   #coding:utf-8
    import os
    import multiprocessing
    import time
    # 寫入 worker
    def inputQ(queue):
    while True:
    info = "進程號 %s :
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved