Courses/Systems Concepts (read-only)

๐Ÿง  Systems Concepts (read-only)

multiprocessing โ€” true parallelism

Bypass the GIL by spawning OS processes.

For CPU-bound work, spawn processes. Each has its own interpreter and GIL, so they run in parallel on multiple cores.

from multiprocessing import Pool

def square(n): return n * n

if __name__ == "__main__":
    with Pool(4) as p:
        print(p.map(square, range(8)))
# [0, 1, 4, 9, 16, 25, 36, 49]

**When to reach for it**

  • Heavy math / image processing โ†’ Pool or ProcessPoolExecutor.
  • Data must cross process boundaries โ†’ use Queue, Pipe, or shared memory.
  • Always guard the entry point with if __name__ == "__main__": on Windows / macOS spawn-start.

> ๐Ÿงช Concept-only here โ€” the browser sandbox runs one process. Try the snippet in real CPython.

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.