Backend Development 6 min read

10 Practical Python Multiprocessing Scenarios for Inter‑Process Communication

This article presents ten practical Python multiprocessing examples demonstrating various inter‑process communication techniques—including pipes, queues, shared memory, managers, events, semaphores, condition variables, and shared counters—to help developers implement concurrent data exchange and synchronization across processes.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
10 Practical Python Multiprocessing Scenarios for Inter‑Process Communication

When dealing with concurrent programming and data exchange between multiple processes, Python offers several mechanisms for inter‑process communication (IPC). Below are ten practical scenarios with code examples illustrating different IPC methods.

1. Pipe

from multiprocessing import Process, Pipe

def sender(conn):
    conn.send("Hello from sender")
    conn.close()

def receiver(conn):
    message = conn.recv()
    print(message)
    conn.close()

if __name__ == "__main__":
    parent_conn, child_conn = Pipe()
    p1 = Process(target=sender, args=(child_conn,))
    p2 = Process(target=receiver, args=(parent_conn,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

2. Queue

from multiprocessing import Process, Queue

def producer(queue):
    queue.put("Hello from producer")

def consumer(queue):
    message = queue.get()
    print(message)

if __name__ == "__main__":
    queue = Queue()
    p1 = Process(target=producer, args=(queue,))
    p2 = Process(target=consumer, args=(queue,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

3. Shared Memory (Value & Array)

from multiprocessing import Process, Value, Array

def writer(num, arr):
    num.value = 10
    for i in range(len(arr)):
        arr[i] = i

def reader(num, arr):
    print(num.value)
    print(arr[:])

if __name__ == "__main__":
    num = Value('i', 0)
    arr = Array('i', range(10))
    p1 = Process(target=writer, args=(num, arr))
    p2 = Process(target=reader, args=(num, arr))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

4. Manager Dictionary

from multiprocessing import Process, Manager

def worker(d):
    d["message"] = "Hello from worker"

if __name__ == "__main__":
    with Manager() as manager:
        d = manager.dict()
        p = Process(target=worker, args=(d,))
        p.start()
        p.join()
        print(d["message"])

5. Event

from multiprocessing import Process, Event

def worker(event):
    event.wait()
    print("Event is set")

if __name__ == "__main__":
    event = Event()
    p = Process(target=worker, args=(event,))
    p.start()
    event.set()
    p.join()

6. Semaphore

from multiprocessing import Process, Semaphore

def worker(semaphore):
    semaphore.acquire()
    print("Worker is running")
    semaphore.release()

if __name__ == "__main__":
    semaphore = Semaphore(1)
    p = Process(target=worker, args=(semaphore,))
    p.start()
    p.join()

7. Condition Variable

from multiprocessing import Process, Condition

def producer(condition):
    with condition:
        condition.notify()
        print("Producer is running")

def consumer(condition):
    with condition:
        condition.wait()
        print("Consumer is running")

if __name__ == "__main__":
    condition = Condition()
    p1 = Process(target=producer, args=(condition,))
    p2 = Process(target=consumer, args=(condition,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

8. Manager Queue (Shared Queue)

from multiprocessing import Process, Manager

def producer(queue):
    queue.put("Hello from producer")

def consumer(queue):
    message = queue.get()
    print(message)

if __name__ == "__main__":
    with Manager() as manager:
        queue = manager.Queue()
        p1 = Process(target=producer, args=(queue,))
        p2 = Process(target=consumer, args=(queue,))
        p1.start()
        p2.start()
        p1.join()
        p2.join()

9. Manager Dictionary (Shared Dict)

from multiprocessing import Process, Manager

def worker(d):
    d["message"] = "Hello from worker"

if __name__ == "__main__":
    with Manager() as manager:
        d = manager.dict()
        p = Process(target=worker, args=(d,))
        p.start()
        p.join()
        print(d["message"])

10. Shared Counter (Value with Lock)

from multiprocessing import Process, Value

def worker(counter):
    with counter.get_lock():
        counter.value += 1

if __name__ == "__main__":
    counter = Value('i', 0)
    processes = [Process(target=worker, args=(counter,)) for _ in range(10)]
    for p in processes:
        p.start()
    for p in processes:
        p.join()
    print(counter.value)

These examples cover a wide range of IPC mechanisms, providing developers with ready‑to‑run patterns for building robust concurrent Python applications.

backendPythonconcurrencyMultiprocessinginterprocess communication
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.