Real Multithreading is Coming to Python — Learn How You Can Use It Now
<p>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 <em>“Per-Interpreter GIL”</em> (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.</p>
<h1>Sub-Interpreters</h1>
<p>Let’s first explain how this <em>“Per-Interpreter GIL”</em> solves Python’s lack of proper concurrency.</p>
<p>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 <code>threading</code> module) only one thread at a time will run.</p>
<p>With the introduction of <em>“Per-Interpreter GIL”</em>, individual Python interpreters don’t share the same GIL anymore. This level of isolation allows each of these sub-interpreters to run <em>really</em> 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).</p>
<p>For a more in-depth explanation, see <a href="https://peps.python.org/pep-0684/" rel="noopener ugc nofollow" target="_blank">PEP 684</a> which describes this feature/change.</p>
<h1>Setup</h1>
<p>To use this new, <em>bleeding edge</em> feature, we need to install up-to-date Python version, that requires building from source:</p>
<p><a href="https://betterprogramming.pub/real-multithreading-is-coming-to-python-learn-how-you-can-use-it-now-90dd7fb81bdf">Read More</a></p>