No history yet

Introduction to Multiprocessing

The One-Core Limit

Modern computers are powerful. Even your phone likely has multiple processing cores, each one a tiny engine ready to do work. Yet, a standard Python program often uses only one of these cores, leaving the others idle. It's like having a team of four chefs in a kitchen, but a rule says only one can cook at a time. The other three just wait their turn. Why does this happen?

The reason is something called the Global Interpreter Lock, or GIL. In CPython, the most common Python implementation, the GIL is a rule that allows only one thread to execute Python code at any given moment. This lock simplifies memory management and prevents certain types of conflicts, but it creates a major bottleneck for tasks that require a lot of computational power.

The GIL is a mechanism in the CPython interpreter that ensures that only one thread can execute Python bytecode at a time.

Because of the GIL, multithreading in Python doesn't achieve true parallelism for CPU-heavy tasks. Threads are great when a program is waiting for something, like a file to download or a database to respond. One thread can wait while another does a little work. But if the task is pure calculation, like processing a huge dataset, the threads end up waiting in line to use the single available core, one after another.

True Parallelism with Multiprocessing

So how do we get all our chefs cooking at once? We give each of them their own kitchen. This is the core idea behind multiprocessing. Instead of creating multiple threads within one process, Python's multiprocessing module creates entirely new processes. Each new process gets its own Python interpreter and its own memory, completely independent of the others. Most importantly, each process has its own GIL.

The multiprocessing module offers a solution by creating separate processes, each with its own Python interpreter and memory space.

By sidestepping the GIL, multiprocessing allows your Python program to use multiple CPU cores simultaneously. If you have four cores, you can run four processes in parallel, dramatically speeding up heavy computational work.

This doesn't mean multithreading is useless. It’s simply a different tool for a different job. The key is knowing when to use each approach.

TechniqueBest for...Key Idea
MultithreadingI/O-bound tasksConcurrent waiting. Good for network requests or reading files.
MultiprocessingCPU-bound tasksParallel execution. Good for heavy math or data processing.

Choosing multiprocessing means you're opting for raw power over fine-grained coordination within a single process. For any task that can be broken down into independent, calculation-heavy chunks, it's the right choice to unlock your hardware's full potential.