The Global Interpreter Lock (GIL)
The GIL has been mentioned in this book several times already, but we have not really covered it in detail and it really does need a bit more explanation before we continue with this chapter.
In short, the name already explains what it does. It is a global lock for the Python interpreter so it can only execute a single statement at once. A lock or mutex (mutual exclusion) in parallel computing is a synchronization primitive that can block parallel execution. With a lock, you can make sure that nobody can touch your variable while you are working on it.
Python offers several types of synchronization primitives, such as threading.Lock
and threading.Semaphore
. These are covered in more detail in the Sharing data between threads and processes section of this chapter.
That means that even with the threading
module, you are still only executing a single Python statement at the same time. So, when it comes to pure Python code, your multithreaded...