Courses/Systems Concepts (read-only)

๐Ÿง  Systems Concepts (read-only)

multiprocessing Queue & Pipe

Send data safely between processes.

Processes don't share memory by default. Queue and Pipe are the two built-in IPC primitives.

from multiprocessing import Process, Queue

def worker(q):
    while True:
        item = q.get()
        if item is None: break    # sentinel: shut down
        print("worked", item * item)

if __name__ == "__main__":
    q = Queue()
    p = Process(target=worker, args=(q,))
    p.start()
    for i in range(5): q.put(i)
    q.put(None)                   # signal end
    p.join()

**Pick the right tool**

  • Queue โ€” many producers / many consumers, thread- & process-safe.
  • Pipe() โ€” two-endpoint, lower overhead, one reader + one writer.
  • Manager().dict()/list() โ€” shared mutable structures (slower; proxies over IPC).
  • shared_memory.SharedMemory โ€” zero-copy buffers for big NumPy arrays.

> ๐Ÿงช Concept-only โ€” Pyodide is single-process.

Concept lesson

This topic relies on OS features the in-browser Python sandbox can't run (threads, subprocesses, sockets). Read the examples here, then try them in real CPython on your machine.

Sign in to track your progress across lessons.