The Python GIL: One Thread at a Time

The Python Global Interpreter Lock (GIL) is a mutex ensuring only one thread executes Python bytecode at a time. This serializes CPU-bound threads, but the lock is released during I/O, making it effective for network-bound tasks.
Why it exists
CPython's memory management is not inherently thread-safe. To prevent race conditions and memory corruption from multiple threads modifying Python objects simultaneously, the GIL was introduced. It acts as a single, global lock, which was a simpler and more performant solution for single-threaded programs than adding complex, fine-grained locks to every data structure.
The mental model
Imagine the Python interpreter is a small room with a single microphone (the GIL). No matter how many people (threads) are in the room, only the person holding the microphone can speak (execute Python bytecode). If a speaker needs to wait for a delivery from outside (an I/O operation), they put down the microphone, allowing someone else to pick it up and speak.
How it works
The GIL is a mutex that a thread must acquire before it can execute Python bytecode. The interpreter forces the running thread to release the GIL periodically, such as after a fixed number of instructions or when the thread blocks on an I/O operation like reading a file or making a network call. This allows other threads to run, creating concurrency but not true parallelism, as only one thread executes at any given instant.
When to use it
Despite the GIL, Python's threading module is highly effective for I/O-bound applications. If your program spends most of its time waiting for network responses, database queries, or disk access, threads are ideal. While one thread is blocked waiting for I/O, the GIL is released, and another thread can run, significantly improving application throughput.
When not to use it
Do not use the threading module for CPU-bound tasks if your goal is to reduce computation time by using multiple cores. Heavy numerical calculations, data compression, or image processing will not see a speedup. The GIL ensures only one thread's Python code runs at a time, creating a performance bottleneck. For these scenarios, use the multiprocessing module, which spawns separate processes, each with its own interpreter and memory space, thus bypassing the GIL.
One canonical example
A web server built with FastAPI or Flask handling concurrent user requests. When one request handler thread is blocked waiting for a database query to complete (an I/O operation), the GIL is released. The server can then switch to another thread to process a different incoming request, achieving high concurrency for an I/O-bound workload.
Interview question
What is the primary reason Python's threading module can improve performance for I/O-bound applications despite the Global Interpreter Lock (GIL)?
- a.Python's threading module automatically distributes I/O operations across multiple CPU cores for parallel execution.
- b.I/O-bound tasks inherently do not execute Python bytecode, thus making the GIL irrelevant to their performance.
- c.The GIL is completely bypassed when Python code interacts with external I/O devices.
- d.The GIL is released by a thread when it enters a waiting state for an I/O operation, allowing other threads to execute Python bytecode.Correct
Why? this is the answer
The card explains that when a thread is blocked waiting for an I/O operation, the GIL is released, allowing other threads to acquire it and execute Python bytecode, thereby improving concurrency. Option C is incorrect because the GIL is released and reacquired, not entirely bypassed.
Just read this? Test yourself on what you have been reading.
Read the original → docs.python.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles