Using Multiprocessing Queues and Pipes

Inter-Process Communication in Python

Learn how to coordinate multiple processes effectively using Python’s multiprocessing Queues, Pipes, and shared memory objects. This guide provides practical examples and best practices for inter-process communication.

Programming
Author
Affiliation
Published

February 5, 2024

Modified

February 7, 2025

Keywords

multiprocessing queues, multiprocessing pipes, Python interprocess communication, shared memory Python, multiprocessing IPC

Introduction

When working with Python’s multiprocessing module, coordinating communication between processes is crucial for building efficient parallel applications. In this tutorial, we’ll explore inter-process communication (IPC) using queues and pipes, and touch on shared memory objects as a complementary approach. You’ll learn how to use these tools to exchange data between processes effectively and avoid common pitfalls.

Multiprocessing Queues

Queues are a thread- and process-safe way to exchange data between processes. They allow you to enqueue data in one process and dequeue it in another.

Example: Using a Queue

import multiprocessing

def worker(q):
    # Put a message into the queue
    q.put("Hello from worker!")

if __name__ == "__main__":
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=worker, args=(q,))
    p.start()
    # Retrieve the message from the queue
    message = q.get()
    print("Message from worker:", message)
    p.join()
Note

Note:
Queues handle synchronization internally, making them a convenient choice for inter-process communication without additional locking mechanisms.

Multiprocessing Pipes

Pipes provide a two-way communication channel between two processes. They are simpler than queues but are typically used for point-to-point communication.

Example: Using a Pipe

import multiprocessing

def worker(conn):
    # Send a message through the pipe
    conn.send("Hello from worker!")
    conn.close()

if __name__ == "__main__":
    parent_conn, child_conn = multiprocessing.Pipe()
    p = multiprocessing.Process(target=worker, args=(child_conn,))
    p.start()
    # Receive the message from the pipe
    message = parent_conn.recv()
    print("Message from worker:", message)
    p.join()
Tip

Tip:
Pipes are ideal for simple, direct communication between two processes, but for more complex scenarios with multiple producers and consumers, queues are often preferable.

Shared Memory Objects

In addition to queues and pipes, Python provides shared memory objects (such as multiprocessing.Value and multiprocessing.Array) to share data between processes without the need for serialization. This can reduce communication overhead.

Example: Using a Shared Value

import multiprocessing

def increment(shared_val):
    # Use a lock to prevent race conditions
    with shared_val.get_lock():
        shared_val.value += 1

if __name__ == "__main__":
    shared_val = multiprocessing.Value('i', 0)
    processes = []
    for _ in range(5):
        p = multiprocessing.Process(target=increment, args=(shared_val,))
        processes.append(p)
        p.start()
    for p in processes:
        p.join()
    print("Final value:", shared_val.value)
Warning

Warning:
When using shared memory objects, always use locks or other synchronization mechanisms to avoid race conditions.

Conclusion

Inter-process communication is a key aspect of building robust multiprocessing applications. Queues and pipes provide straightforward ways to exchange data between processes, while shared memory objects offer an efficient solution for sharing large data structures without excessive overhead. By understanding and applying these tools, you can design Python applications that are both efficient and scalable.

Further Reading

Happy coding, and may your processes communicate efficiently!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Using {Multiprocessing} {Queues} and {Pipes}},
  date = {2024-02-05},
  url = {https://www.datanovia.com/learn/programming/python/advanced/parallel-processing/multiprocessing-queues-and-pipes.qmd},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Using Multiprocessing Queues and Pipes.” February 5, 2024. https://www.datanovia.com/learn/programming/python/advanced/parallel-processing/multiprocessing-queues-and-pipes.qmd.