How to Send and Receive Messages Between Processes with multiprocessing.Pipe in Python
Use multiprocessing.Pipe to create a two-way connection between two processes, send a message from parent to child, and receive a reply back.
Python code
21 linesimport multiprocessing
def child_process(conn):
"""Receive from parent and send back a response."""
message = conn.recv()
print(f"Child received: {message}")
conn.send("Hello from child!")
if __name__ == "__main__":
parent_conn, child_conn = multiprocessing.Pipe()
process = multiprocessing.Process(target=child_process, args=(child_conn,))
process.start()
parent_conn.send("Hello from parent!")
response = parent_conn.recv()
print(f"Parent received: {response}")
process.join()
Output
Child received: Hello from parent!
Parent received: Hello from child!
How it works
multiprocessing.Pipe() returns two connection objects, one for each end of the channel. Passing one end to a child process while keeping the other in the parent creates a communication bridge. The child calls conn.recv() to block until a message arrives, processes it, then sends a response via conn.send(). The parent sends first, then waits with recv() for the reply. Finally, process.join() ensures the program exits cleanly after the child finishes.
Common mistakes
- Forgetting to pass the correct connection end to the child process
- Calling `recv()` on the same connection that was used for `send()` without having an opposite end
- Not joining the process, causing the program to exit before the child completes
Variations
- Use `conn.send_bytes()` and `conn.recv_bytes()` for binary data instead of pickled objects.
- Use `duplex=False` to create a one-way pipe where only the parent can send and the child can receive (or vice versa).
Real-world use cases
- Sending a job to a worker process and receiving the computed result back.
- Implementing request-reply communication inside a producer-consumer workflow.
- Passing configuration data to a child process and getting a status update after initialization.
Sponsored
More from Concurrency & performance
- Benchmark list.append vs deque.append in Python medium
- Build a Python Performance Profiler That Generates Readable Reports medium
- Graceful Shutdown Executor Context Manager in Python medium
- How to Build a Producer-Consumer Pattern with asyncio.Queue in Python medium
- How to Cancel an asyncio Task with Graceful Cleanup in Python medium
- How to Convert Data in Parallel with ThreadPoolExecutor in Python easy
Keep learning
Related tutorials and quizzes for this topic.