The GIL and its effects on Python multithreading
As you probably know, the GIL stands for the Global Interpreter Lock, and its job is to make the CPython interpreter thread-safe. The GIL allows only one OS thread to execute Python bytecode at any given time, and the consequence of this is that it's not possible to speed up CPU-intensive Python code by distributing the work among multiple threads. This is, however, not the only negative effect of the GIL. The GIL introduces overhead that makes multi-threaded programs slower, and what is more surprising, it can even have an impact on I/O-bound threads. In this post I'd like to tell you more about non-obvious effects of the GIL. Along the way, we'll discuss what the GIL really is, why it exists, how it works, and how it's going to affect Python concurrency in the future. Note: In this post I'm referring to CPython 3.9. Some implementation details will certainly change as CPython evolves. I'll try to keep track of important changes and add update notes. Let me first remind you what Pytho
As you probably know, the GIL stands for the Global Interpreter Lock, and its job is to make the CPython interpreter thread-safe. The GIL allows only one OS thread to execute Python bytecode at any given time, and the consequence of this is that it's not possible to speed up CPU-intensive Python code by distributing the work among multiple threads. This is, however, not the only negative effect of the GIL. The GIL introduces overhead that makes multi-threaded programs slower, and what is more surprising, it can even have an impact on I/O-bound threads. In this post I'd like to tell you more ab
related reading
- What Is the Python Global Interpreter Lock (GIL)? – Real Pythonrealpython.com
- PEP 703 – Making the Global Interpreter Lock Optional in CPython | peps.python.orgpeps.python.org
- Global interpreter lock - Wikipediaen.wikipedia.org
- Running Python Parallel Applications with Sub Interpreterstonybaloney.github.io
- Multithreading and Multiprocessing in 10 Minutes | Towards Data Sciencetowardsdatascience.com
- An Intro to Threading in Python – Real Pythonrealpython.com
- Why Learn Python Concurrency – SuperFastPythonsuperfastpython.com
- Notes on structured concurrency, or: Go statement considered harmful - njs blogvorpus.org
- (Quite) A Few Words About Asyncyoric.github.io
- Using Threads to Run Code Simultaneously - The Rust Programming Languagedoc.rust-lang.org
- Speeding up your code when multiple cores aren’t an optionpythonspeed.com
- python - How does asyncio actually work? - Stack Overflowstackoverflow.com