Global Interpreter Lock (GIL)
The Global Interpreter Lock (GIL) is a lock used in the CPython implementation 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 multi-threaded 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.
Example
Here’s an example to illustrate the effect of the GIL in a multi-threaded 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)?
Python's Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter at any one time. In this article you'll learn how the GIL affects the performance of your Python programs.
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)
- Logging in Python (Tutorial)
- Understanding Python's Global Interpreter Lock (GIL) (Course)
- Threading in Python (Course)
- Python Threading (Quiz)
- Logging Inside Python (Course)
- Logging in Python (Quiz)