Sunday, 20 December 2015

Python: multiprocessing: pipe: Exchange data between processes

Pipe has two ends, from one end we can send data and from other end we can receive data.  By default pipe support two-way communication, you can send, receive from either end. Pipe class is not synchronized, the data in pipe may correct, if more than one process is trying to operate.

multiprocessing.Pipe([duplex])

Returns a pair of connection objects(conn1, conn2) representing the ends of pipe. Argument duplex is optional, if duplex is true, the pipe is duplex (bidirectional). If duplex is false, then pipe is unidirectional, where conn1 sends data and conn2 receive data.
from multiprocessing import Process, Pipe

def writeToConnection(conn):
    conn.send([2, 3, 5, 7])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=writeToConnection, args=(child_conn,))
    p.start()
    print(parent_conn.recv())
    p.join()




Previous                                                 Next                                                 Home

No comments:

Post a Comment