Communication between processes using Pipe
A pipe can have only two endpoints. Hence, it is preferred over queue when only two-way communication is required.
import multiprocessing def sender(conn: multiprocessing.connection.Connection, msgs: list): """ function to send messages to other end of pipe """ for msg in msgs: conn.send(msg) print("Sent the message: {}".format(msg)) conn.close() def receiver(conn: multiprocessing.connection.Connection): """ function to print the messages received from other end of pipe """ while 1: msg = conn.recv() if msg == "END": break print("Received the message: {}".format(msg)) if __name__ == "__main__": # messages to be sent msgs = ["hello", "hey", "hru?", "END"] # creating a pipe parent_conn, child_conn = multiprocessing.Pipe() # creating new processes p1 = multiprocessing.Process(target=sender, args=(parent_conn, msgs)) p2 = multiprocessing.Process(target=receiver, args=(child_conn,)) # running processes p1.start() p2.start() # wait until processes finish p1.join() p2.join()
References
https://www.geeksforgeeks.org/multiprocessing-python-set-2/