Global Interpreter Lock(GIL)
The Python 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.
Problem GIL solve for Python
Python uses reference counting for memory management. It means that objects created in Python have a reference count variable that keeps track of the number of references that point to the object. When this count reaches zero, the memory occupied by the object is released.
Why lock is global?
This reference count variable can be kept safe by adding locks to all data structures that are shared across threads so that they are not modified inconsistently.
But adding a lock to each object or groups of objects means multiple locks will exist which can cause another problem—Deadlocks (deadlocks can only happen if there is more than one lock). Another side effect would be decreased performance caused by the repeated acquisition and release of locks.
Why Hasn’t GIL Been Removed Yet?
- other solutions decrease performance of single-threaded and multi-threaded I/O-bound programs and some of them are just too difficult.
How to deal with Python’s GIL
- Use multiprocessing module
- Use alternative Python interpreters
- CPython(written in C)
- Jython(written in Java)
- IronPython(written in C#)
- PyPy(written in Python)