Python is 32 years old language, yet it still doesn’t have proper, true parallelism/concurrency. This is going to change soon, thanks to the introduction of a “Per-Interpreter GIL” (Global Interpreter Lock) which will land in Python 3.12. While the release of Python 3.12 is some months away, the code is already there, so let’s take an early peek at how we can use it to write truly concurrent Python code using sub-interpreters API.
Sub-Interpreters
Let’s first explain how this “Per-Interpreter GIL” solves Python’s lack of proper concurrency.
Simply put, GIL or Global Interpreter Lock is a mutex that allows only one thread to hold control of the Python interpreter. This means that even if you create multiple threads in Python (e.g. using threading module) only one thread at a time will run.
With the introduction of “Per-Interpreter GIL”, individual Python interpreters don’t share the same GIL anymore. This level of isolation allows each of these sub-interpreters to run really concurrently. This means, that we can bypass Python’s concurrency limitations by spawning additional sub-interpreters, where each of them will have its own GIL (global state).
For a more in-depth explanation, see PEP 684 which describes this feature/change.
Setup
To use this new, bleeding edge feature, we need to install up-to-date Python version, that requires building from source: