A data engineer has a script that computes checksums for 400 large files. It takes eight minutes on one core of a 16-core machine. She rewrites it with a pool of 8 threads, expecting something close to one minute. It still takes nearly eight minutes, and the CPU graph shows one core busy while the rest idle. The next day she uses 8 threads to download 400 files from a storage bucket, and that version is about seven times faster. Same language, same thread pool, opposite results. The reason is CPython’s global interpreter lock, the GIL.
What the GIL is
CPython, the standard Python implementation, has a single mutex that a thread must hold to execute Python bytecode. Only one thread can hold it at a time. You can create as many threads as you like, and the operating system will schedule them on different cores, but at any instant only one of them is actually running Python code. The others wait for the lock.
The lock exists mainly because of how CPython manages memory. Every object carries a reference count, which is updated constantly as variables are assigned and released. If two threads changed the same count simultaneously without protection, counts would be corrupted and objects freed while still in use. One global lock is a simple way to make all of that safe, and it also made writing C extensions easier for decades. The cost is that pure Python code cannot use more than one core within a single process.
Why I/O-bound threads still work
A thread releases the GIL whenever it does a blocking operation such as reading from a socket, waiting on a file or sleeping. While thread A waits 300 milliseconds for a storage server to reply, threads B through H can take turns running Python code. Since most of the downloading script’s time was spent waiting on the network, eight threads could overlap their waits and finish much sooner.
A thread doing pure computation never voluntarily waits, so the interpreter forces switches periodically. The default switch interval is 5 milliseconds, visible through sys.getswitchinterval. Threads take turns on one lock, so CPU-bound work spread across threads runs at roughly single-core speed, sometimes slightly slower because of the switching overhead.
The checksum example has a twist. Some hashing functions in Python’s standard library release the GIL while processing large buffers, because the heavy work happens in C and does not touch Python objects. Many C extensions do the same; NumPy releases the GIL during many array operations. So whether threads help with “CPU work” depends on whether that work runs in Python bytecode or inside a C routine that releases the lock. Measuring is the only reliable answer.
A common misunderstanding: the GIL is not thread safety
Because only one thread runs at a time, people sometimes assume Python threads cannot have race conditions. They can. Incrementing a shared counter looks like one step but compiles to several bytecode instructions: load the value, add one, store the result. A thread switch can happen between the load and the store, so two threads can both read 41 and both write 42, losing an update. The GIL protects the interpreter’s internals, not your program’s logic. Shared mutable data still needs a lock or a thread-safe structure such as a queue.
Choosing a concurrency tool
| Workload | Good fit | Why |
|---|---|---|
| Many network calls, file I/O, database queries | Threads (for example ThreadPoolExecutor) or asyncio | Waiting releases the GIL; overlap the waits |
| Thousands of concurrent connections | asyncio | One thread, cheap coroutines, no per-connection thread cost |
| Pure-Python computation | multiprocessing or ProcessPoolExecutor | Each process has its own interpreter and its own GIL |
| Numerical work on arrays | NumPy, or libraries built on it | Heavy work runs in C, often releasing the GIL |
| Very large CPU jobs | Multiple processes or machines (Celery workers, Spark, Dask) | Scale beyond one machine |
Processes have real costs. Each one carries its own memory for the interpreter and loaded modules, and data passed between processes must be serialised, by default with pickle. Sending a 500 MB DataFrame to each worker can cost more than the computation it was meant to speed up. A better pattern is to send small instructions, such as file paths or row ranges, and let each worker load its own data. Process start-up method also matters: forking is fast but can copy awkward state such as open connections or threads, and Python has been moving its defaults on Linux away from plain fork towards safer start methods in recent versions.
This is also why Python web servers such as Gunicorn and uWSGI commonly run several worker processes per machine. Each process gets its own GIL, so a server with 8 cores can run 8 or more workers and use all of them, even though each worker serves Python code on one core at a time.
The GIL is becoming optional
The situation is changing. PEP 703 proposed making the GIL optional, and Python 3.13 shipped an experimental “free-threaded” build in which threads can run Python code truly in parallel. In Python 3.14 the free-threaded build moved to officially supported status, though it remains a separate build rather than the default. Removing the GIL required reworking reference counting and making internal structures thread-safe, and single-threaded code can run somewhat slower on the free-threaded build. C extensions also need to declare that they are compatible.
Separately, Python 3.12 introduced a per-interpreter GIL, allowing multiple sub-interpreters within one process each with its own lock, and Python 3.14 exposed this to ordinary Python code through a standard-library interpreters module. Both developments point the same way: parallelism in Python is getting easier, but for production systems today, check which build and libraries you are running before assuming threads will use multiple cores.
A quick decision habit
Before adding concurrency to Python code, profile and ask where the time goes. If it goes to waiting, threads or asyncio will help. If it goes to Python bytecode, threads will not, and processes, vectorised libraries or a free-threaded build are the options. If it goes into a C library, check whether that library releases the GIL. Most disappointing speed-ups in Python come from skipping that one question.
Written by the RP Strativa Team. File counts and timings in the examples are illustrative.
Leave a Reply