Sunday, 20 December 2015

Python: Multiprocessing: Exchange data between processes

By using Queues and Pipes you can exchange data between two processes.

Using Queues

Queues are thread and process safe.
from multiprocessing import Process, Queue

def addToQueue(queue):
    queue.put([2, 3, 5, 7, 11])

if __name__ == '__main__':
    queue = Queue()
    p = Process(target=addToQueue, args=(queue,))
    p.start()
    print(queue.get())
    p.join()

Using Pipes

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. Pipes are not thread safe.
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