The Global Interpreter Lock (GIL) is a mutex or a lock that ensures only one thread can use the CPU at a given time.
- For Python to execute bytecode, it has to acquire a single lock for the interpreter itself, which is the GIL
- The presence of a single lock removes the possibility of deadlocks
- The GIL doesn’t increaase performance overhead
- The side effect is that CPU-bound programs end up becoming single-threaded
What’s bizarre to me is that this is counter-intuitive to my understanding of multi-threading. The point of threads is that they can be processed in parallel using multiple CPU cores and yet the GIL restricts CPU resources to just one thread at once. There’s a lot of discussion and debate about this in the Python community and it seems that the GIL will be made optional starting from Python 3.13
The GIL’s primary purpose is to prevent race conditions caused by multi-threading that can lead to memory leaks