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:
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 += amountThis proof of concept is implemented with subinterpreters and anyone can try it via the Python Package Index. The install command:
python -m pip install bocpyThe 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.


