Table of contents
GIL
GIL ensure that only one thread executes Python bytecode at a time, which means it prevents multiple threads from executing Python code concurrently.
GIL works like this:
There are three threads, but they are not running concurrently. One needs to release in order for other threads to acquire.
So, simply GIL is a limitation. However, you don’t have to worry because python is fast enough even though only one thread executes at a time.
Purpose of Using it
Using GIL in CPython is to solve problems that arise when managing memories. Python manages memories by a reference counting method. Every object in Python has an associated reference count. This count tracks how many references (variables, function arguments, etc.) point to the object. When the count becomes 0, Python automatically deallocates the object and free the memory.
If multiple threads modify referecne count at the same time, it may result in a race condition.
That’s why GIL is used. CPython locks the interpreter globally, so that one thread executes at a time.
If You Don’t Want GIL
You can do a multiprocessing or use Jython and PyPy.