Free-threaded CPython

From Ralf Gommers:

You may be wondering by now what “free threading” or “free-threaded CPython” is, and why you should care. In summary: it is a major change to CPython that allows running multiple threads in parallel within the same interpreter. It is becoming available as an experimental feature in CPython 3.13. A free-threaded interpreter can run with the global interpreter lock (GIL) disabled – a capability that is finally arriving as a result of the efforts that went into PEP 703 – Making the Global Interpreter Lock Optional in CPython.

Why? Performance. Multi-threaded performance. It makes it significantly easier to write code that efficiently runs in parallel and will utilize multiple CPU cores effectively. The core counts in modern CPUs continue to grow, while clock speeds do not grow, so multi-threaded performance will continue to grow in importance.

How? It’s now easy to get started by installing a free-threaded interpreter: macOS/Linux/Windows & python.org/pyenv/apt/yum/conda – your preferred option is probably available now.

That’s great, but here’s why the roll-out is being done carefully:

Thread-safety. While pure Python code should work unchanged, code written in other languages or using the CPython C API may not. The GIL was implicitly protecting a lot of thread-unsafe C, C++, Cython, Fortran, etc. code – and now it no longer does. Which may lead to all sorts of fun outcomes (crashes, intermittent incorrect behavior, etc.).

Compatibility status tracking for popular Python packages and other information is available here.

As it became tougher and tougher to ramp up the clock speed of single CPU cores, increasing the core count became the standard approach. And, Python’s GIL has been a huge performance bottleneck. This is a welcome feature and not to mention—a major change!


Posted

in

by

Tags:

Discover more from Unixism

Subscribe now to keep reading and get access to the full archive.

Continue reading