Python... What the GIL?
Are you confused by Python's Global Interpreter lock? or if you're trying to speed up your ๐ code... you'd know the basics about the GIL is all about.
Nowadays computers have multiple CPU cores that help speed up the different applications we run, usually by running tasks in parallel and relying on threads.
But when we talk about Python & even if youโre explicitly defining a thread, your code might not take full advantage of all CPU cores, and thatโs where the GIL comes to play.
The GIL is a lock that only lets one thread have full control of the Python interpreter.
Letโs review the following example:
We run the slow task from the main thread and with a thread.
The slow task does a loop for 5 seconds before completing it.
As you might expect, it takes roughly 5 seconds
But if we dig deeper, weโd see that the execution is being switched between the main thread and the thread we created.
โฆand that happens because:
The GIL can be held only by one thread at a time.
The thread releases the GIL every 5ms by default.
We can arbitrarily tell Python to release the GIL every 10 seconds by running the following code
So, let us run it again, but let python release the GIL every 10 seconds and see what happens
Now the total duration is 10 seconds because we are not letting python make progress in each thread every 5ms.
Crazy right, but here is what you need to remember
The switch back and forth between threads lets us make progress in all threads, but not at its full potential.
There is no guarantee that a thread is going to release the GIL every 5ms.
If a thread holds the GIL, other threads canโt run until the GIL is released.
Libraries that rely on CPython require the GIL.
Use libraries that use the GIL correctly or rely as few as possible on the CPython.
For example Numpy
If you write a long-running task, make sure your code does not hold the GIL.
As a general thought, donโt try to optimize โverticallyโ if you arenโt facing the problem yet, in other words, if you want to speed up a task with parallelism, and if you arenโt getting the expected results, now itโs a good moment to unveil your GIL knowledge and start to measure how the code is being executed by relying on tools like https://viztracer.readthedocs.io/en/latest/index.html to generate graphs and analyze the execution of your code.