Python Central HubSign in

Python Programming / Python MultiProcessing

Multiprocessing in Python

Learn how multiprocessing works, why it bypasses the GIL, and when to use it for CPU-bound tasks.

What is multiprocessing?

Multiprocessing runs work in multiple processes.

  • Each process has its own Python interpreter and memory space.
  • This can use multiple CPU cores.

Why multiprocessing helps CPU-heavy work

In CPython, threads are limited by the GIL for CPU-bound code.

Multiprocessing avoids this because:

  • each process has its own GIL

When to use multiprocessing

Best for CPU-bound tasks:

  • heavy number crunching
  • image processing
  • data transformations
  • simulations

For I/O-bound tasks, threading or async may be better.

Important note (Windows / notebooks)

When using multiprocessing, always guard code with:

Python
if __name__ == "__main__":
    # start processes here
    pass

This avoids infinite child-process spawning on some platforms.

Visualize it

Unlike threads, which share one interpreter and memory space, each process gets its own — so they run truly in parallel on separate CPU cores with no GIL to fight over:

diagram Each process runs in parallel, in its own memory mermaid
A parent process spawns three child processes, each with its own Python interpreter and memory, running in parallel on separate CPU cores. Data must be sent explicitly (pickled) between them, since nothing is shared.

🧪 Try It Yourself

Exercise 1 – Start a Process

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Process Pool map()

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Multiprocessing Queue

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did