Skip to main content
LeoNex News

Free-threaded Python (no GIL): the next phase

Illustration: Free-threaded Python (no GIL): the next phase
In this article

At the Python Language Summit, Tobias Wrigstad, Fridtjof Stoldt and Donghee Na returned to the topic of Python's post-free-threading era and the question of which high-level concurrency primitives the language should offer. The team proposes the Behavior-Oriented Concurrency (BOC) model, built on tasks and mutexes that know which data they protect. They also announced bocpy, a proof of concept for experimentation. The discussion that followed showed that the community still has many reservations about settling on a single approach.

Why threads are not yet a good interface

Today, the interface for using free-threading is the thread. It is a familiar concept, since people usually encounter true parallelism through school and textbooks. However, the team asks whether threads are a good user interface at all, because using threads forces programmers to worry about deadlocks and race conditions.

Python's free-threading project has made significant progress since PEP 779 was accepted. But one criterion in that acceptance decision remains unresolved: high-level concurrency primitives. The Steering Council wanted the core team to begin considering and proposing primitives that users can use safely and efficiently without needing a deep understanding of the underlying threading mechanisms, while also recommending the concurrent package in the stdlib where appropriate. There is precedent from other languages:

  • Rust, with ownership and borrowing.

  • Go, with channels and goroutines.

  • Erlang, with actors and message passing.

Three axes: fast, simple or safe

The talk compares how languages implement concurrency along three axes that sometimes conflict with one another: speed, simplicity and safety. By this comparison, C is fast and simple thanks to direct memory access through pointers, but this allows dangerous operations and relies on programmer discipline. Erlang copies data to another actor when communicating, so it is safe and simple, but performance suffers. Rust is both fast and safe but not simple, because users often struggle with the borrow checker.

For Python, the team places each model on the same triangle:

  • The Global Interpreter Lock (GIL) is a simple model but not fully safe, since the GIL only protects the runtime.

  • Subinterpreters are safer thanks to isolation, but they are neither simple nor fast because data must be copied.

  • Free-threaded Python is rather like C: simple and fast, but it places the burden of correctness and safety on the programmer.

From this, the team argues that for a high-level model, safety should come first, then performance, with simplicity third, because simplicity conflicts with performance.

Behavior-Oriented Concurrency and bocpy

The @when decorator lets you declare which mutexes a task depends on; the task accesses data only through those mutexes. This mechanism allows BOC to build a dependency graph between all tasks and data. When a program that follows these constraints is handed to the scheduler, the result is three properties: no deadlocks, no data races and good locking discipline. Users do not need to decide whether to create threads or subinterpreters because the runtime takes care of that, and the scheduler can turn detected deadlocks or data races into exceptions, identifying them before execution.

One example converted from locks and threads to BOC is two bank accounts transferring money to each other and then printing the result. The core of the transfer function:

python
def atomic_transfer (src: Cown[Account], dst: Cown[Account], amount: float ):
 """Move funds from ``src`` to ``dst`` if both are unfrozen and funded."""
 @when (src, dst)
 def do_transfer (src: Cown[Account], dst: Cown[Account], amount = amount):
 src_account = src.value
 dst_account = dst.value
 if src_account.balance > amount:
 src_account.balance -= amount
 dst_account.balance += amount

This proof of concept is implemented with subinterpreters and anyone can try it via the Python Package Index. The install command:

bash
python -m pip install bocpy

The bocpy package provides three features:

  • Cown, that is, a lock with ownership.

  • Behavior, the tasks produced by the @when decorator.

  • Scheduler, implemented with subinterpreters and with planned support for free-threading.

Evaluating bocpy and the next steps

The team assesses bocpy against its own three criteria. On simplicity, the examples show that bocpy is simple. On safety, bocpy currently runs on stable Python and so has only implemented isolation; ownership is not yet there and cannot be built as a third-party library because it requires changes to the runtime. On performance, bocpy does well with programs that are not dominated by communication, since communication between subinterpreters is expensive.

Community discussion

Key takeaways

More in Software Engineering

Illustration: Rust in CPython? Discussion at the Python Language Summit
SOFT-ENG Software Engineering

Rust in CPython? Discussion at the Python Language Summit

David Hewitt, representing the Rust for CPython project team, returned to the Python Language Summit to ask Python's core developers what they want from Rust. He presented a phased roadmap, success criteria and a proposal to start with an optional Rust backend for the zlib module. The long-term goal is to make Rust…

6 min read

Illustration: Notable V8 changes from Node.js 26 to 27
SOFT-ENG Software Engineering

Notable V8 changes from Node.js 26 to 27

The Node.js 27 Alpha upgrades V8 to version 15.6, up from 14.6 in Node.js 26. The upgrade brings several new JavaScript language features, a number of behavior changes, performance improvements and memory adjustments. Some changes may break code that relies on the old behavior, so developers should…

3 min read

Email newsletter

Receive a selection of new LeoNex News articles by email.

We only use this address to send the newsletter.

Receive a selection of new LeoNex News articles by email. You will receive a confirmation email; the subscription only takes effect after you confirm. Every email includes an unsubscribe link. Read the privacy policy