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.

Easy Python 3.9+ Aug 9, 2026 Concurrency & performance 14 views 0 copies

Python code

21 lines
Python 3.9+
import 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

stdout
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

  1. Use `conn.send_bytes()` and `conn.recv_bytes()` for binary data instead of pickled objects.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Concurrency & performance

Related tutorials and quizzes for this topic.