DEV Community

loading...

Best Python GIL overview yet

mzsrtgzr2 profile image Moshe Roth ・1 min read

Most of Python experienced programmers know what is the GIL (Global Interpreter Lock) keeping Python code thread safe no matter what operations we make on shared memory.
Most of python programmers know when to use Multiprocessing vs Multithreading and Asyncio for intensive IO operations.

This article really opened my eyes to why GIL chosen as ultimate solution in CPython's interpreter architecture and how it affected the community when extending CPython/Python:

https://realpython.com/python-gil

In this article I found great example to how CPython is not thread-safe - look for the example with "reference counter", beautiful example.

What I really like is how the writer suggests a potential solution: Use locks for all Objects in memory and lock/unlock every operation it goes through.

In a nutshell, this solution is problematic because it will create dead locks and performance degradation (locks require cpu) - which can turn away many developers.
So the GIL is THE ONE LOCK for all threading operation that can be released by CPYTHON code (mostly happens on IO calls and Thread exit).

Discussion

pic
Editor guide