Global Interpreter Lock (GIL)
The Global Interpreter Lock (GIL) is a lock used in CPython to ensure that only one thread executes Python bytecode at a time. This lock is necessary because CPython’s memory management isn’t thread-safe.
While the GIL makes it easier to implement CPython’s garbage collection, it can be a significant bottleneck in CPU-bound and multithreaded programs. Even if you have multiple threads, the GIL allows only one thread to execute Python code at a time, effectively serializing the execution of threads.
For I/O-bound operations, such as network requests or file I/O, the impact of the GIL is less noticeable because threads spend much of their time waiting for external events. However, for compute-intensive tasks, you might want to consider multiprocessing, which involves separate processes and thus bypasses the GIL.
Starting with Python 3.13, CPython offers an optional free-threaded build (PEP 703) in which the GIL can be disabled, allowing true parallel execution of threads. Free-threading was experimental in Python 3.13, but as of Python 3.14 it’s officially supported (PEP 779), though it still ships as a separate build with its own interpreter, python3.14t, rather than the default CPython build.
Example
Here’s an example to illustrate the effect of the GIL in a multithreaded Python program:
counter.py
import logging
import threading
from time import sleep
logging.basicConfig(
format="%(asctime)s [%(threadName)s] %(message)s",
level=logging.INFO,
datefmt="%H:%M:%S",
)
def count(stop=10):
for i in range(stop):
sleep(0.2) # Simulate work
logging.info(f"count={i}")
# Parameters for threads
threads = []
thread_configs = [
{"name": "Thread 1"},
{"name": "Thread 2"},
]
# Create and start threads
for config in thread_configs:
thread = threading.Thread(
target=count,
name=config["name"],
)
threads.append(thread)
thread.start()
# Wait for threads to complete
for thread in threads:
thread.join()
logging.info("Counting finished.")
In this example, you have two threads that run concurrently. However, due to Python’s GIL, the threads don’t truly execute in parallel when running CPU-bound tasks.
Related Resources
Tutorial
What Is the Python Global Interpreter Lock (GIL)?
The Python GIL is a mutex that lets only one thread control the interpreter at a time. Learn how it shapes your program's performance.
For additional information on related topics, take a look at the following resources:
- Bypassing the GIL for Parallel Processing in Python (Tutorial)
- An Intro to Threading in Python (Tutorial)
- Python Thread Safety: Using a Lock and Other Techniques (Tutorial)
- Threading in Python (Course)
- Understanding Python's Global Interpreter Lock (GIL) (Course)
- What Is the Python Global Interpreter Lock (GIL)? (Quiz)
- Bypassing the GIL for Parallel Processing in Python (Quiz)
- Python Threading (Quiz)
- Thread Safety in Python: Locks and Other Techniques (Course)
- Python Thread Safety: Using a Lock and Other Techniques (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.