Structured concurrency in Python, including free-threaded 3.14
Structured concurrency in Python with pyfect: fibers, bounded fan-out, timeouts that cancel safely, cleanup that always runs, and what free-threaded 3.14 adds.
- python
- structured concurrency
- free-threaded python
- cancellation
- asyncio
Structured concurrency in Python means one rule: concurrent work can't outlive the code that
started it. When a function returns, everything it started has finished, failed, or been
cancelled, and its cleanup has run. Trio introduced the idea to Python with nurseries, and
asyncio added TaskGroup in 3.11. pyfect, a port of Effect to Python
(pip install pyfect-dev), builds its whole runtime on it.
This post covers how that looks in practice: running work concurrently with a limit, timeouts, interruption, finalizers that always run, and resources. It ends with what free-threaded Python 3.14 does and doesn't change.
Fibers
Every pyfect effect runs on a fiber, a lightweight thread managed by pyfect's scheduler. You
can run a program with fx.run_blocking, with no event loop, or inside asyncio with
fx.run_async. The code is the same either way.
eff.fork() starts a child fiber and returns at once. The child is tied to its parent: when
the parent finishes, its children still running are interrupted.
import pyfect as fx
log: list[str] = []
@fx.gen
def heartbeat():
while True:
yield from fx.sleep("10 millis")
@fx.gen
def handle_request():
# The forked fiber is a child: it can't outlive this one.
yield from heartbeat.on_interrupt(fx.sync(lambda: log.append("heartbeat stopped"))).fork()
yield from fx.sleep("35 millis")
log.append("request done")
return 200
assert fx.run_blocking(handle_request) == 200
print(log)The heartbeat loops forever, and nothing in handle_request stops it. It stops anyway, when
the request is done, and its on_interrupt handler runs. A background task that leaks past
the request that started it, still holding a connection, is a common bug in asyncio code that
calls create_task without keeping track of the task. Here it can't happen by default. When
you do want a fiber to outlive its parent, fork_daemon says so explicitly.
Running work concurrently with a limit
fx.for_each(items, f, concurrency=n) runs f on every item, at most n at a time, and
returns the results in input order. fx.all(a, b, c) does the same for a fixed set of effects
and keeps each one's type.
from dataclasses import dataclass
import pyfect as fx
from pyfect.duration import millis
@dataclass
class NotFound(fx.TaggedError):
page: int
def fetch(page: int) -> fx.Effect[str, NotFound]:
if page == 3:
return fx.sleep("10 millis").and_then(fx.fail(NotFound(page)))
stopped = fx.sync(lambda: print(f"page {page} interrupted"))
return fx.sleep(millis(50 * page)).as_(f"page {page}").on_interrupt(stopped)
# At most three fetches at a time; results come back in input order.
print(fx.run_blocking_exit(fx.for_each([1, 2, 4, 5], fetch, concurrency=3)))
# Page 3 fails at 10 ms: the fetches still running are interrupted,
# their finalizers run, and pages 4 to 6 never start.
exit = fx.run_blocking_exit(fx.for_each(range(1, 7), fetch, concurrency=3))
assert isinstance(exit, fx.Failure)
print(exit.cause.find_error())The second run shows what happens on failure. Pages 1, 2 and 3 start. Page 3 fails at 10 ms.
pyfect interrupts the two fetches still running, waits for their on_interrupt handlers, and
never starts pages 4 to 6. The failure comes back as the typed error NotFound(page=3), not
as an exception group you have to pick apart.
Cancel tasks in Python safely: timeouts and interruption
Cancelling work safely has two halves: the work must actually stop, and its cleanup must run
before the caller moves on. In pyfect, a timeout is an interruption, and timeout, race and
race_all all wait for the interrupted side to finish cleaning up before they return.
import pyfect as fx
events: list[str] = []
connection = fx.acquire_release(
fx.sync(lambda: events.append("open") or "conn"),
lambda _conn, exit: fx.sync(lambda: events.append(f"close after {type(exit).__name__}")),
)
@fx.gen
def report():
conn = yield from connection
events.append(f"querying on {conn}")
yield from fx.sleep("10 seconds") # a query that hangs
return "report"
# timeout interrupts the slow effect and waits for its finalizers before failing.
exit = fx.run_blocking_exit(fx.scoped(report).timeout("50 millis"))
assert isinstance(exit, fx.Failure)
print(exit.cause.find_error())
print(events)fx.acquire_release opens the connection and registers its release on a scope, and
fx.scoped closes that scope when report ends. The query hangs, the timeout fires at 50 ms,
and by the time run_blocking_exit returns, the connection is closed. The release function
gets the Exit, so it can tell a clean finish from a failure or an interruption, for example
to roll back a transaction instead of committing it.
Three rules make this dependable:
- Interruption is not an exception. In asyncio, cancellation is a
CancelledErrorraised into your code, and a bareexcept:or a retry loop catchingBaseExceptioncan swallow it. In pyfect, error handlers such ascatch_allnever see an interruption, so a handler can't swallow one by accident. - Acquiring a resource can't be interrupted halfway. Either the acquire finished and the release will run, or it never started.
- Finalizers run in reverse order of acquisition, as they would in nested
withblocks.
When a few steps must not be interrupted halfway, wrap them in fx.uninterruptible_mask: an
interruption then waits until they are done.
Resources with with blocks or scopes
Not every caller is pyfect code. fx.managed(eff) turns a scoped effect into a context
manager, so plain Python gets the same guarantee:
import pyfect as fx
events: list[str] = []
pool = fx.acquire_release(
fx.sync(lambda: events.append("pool opened") or "pool"),
lambda _pool, exit: fx.sync(
lambda: events.append(f"pool closed, interrupted: {exit.has_interrupts}")
),
)
# Plain Python code gets the same guarantee from a with block.
try:
with fx.managed(pool) as p:
events.append(f"using {p}")
raise KeyboardInterrupt # Ctrl-C in the middle of the block
except KeyboardInterrupt:
events.append("KeyboardInterrupt propagated")
print(events)Inside the block, Ctrl-C or an asyncio cancellation counts as an interruption of the scope,
and any other exception as a failure, so the release sees the same Exit it would get inside
pyfect. The exception still propagates out of the with. The
resources guide covers scopes, pools, caches and how a layer can own a
resource for a program's whole life.
What free-threaded Python 3.14 changes
Free-threaded CPython (the 3.14t build) can run Python code on several threads at once without the GIL. It's easy to assume that makes fibers run in parallel. It doesn't, and it's worth being precise about why.
All the fibers of one pyfect run take turns on that run's thread, as Effect's fibers take turns on Node's one thread. That is a design choice, not a GIL limitation, and it's what makes the guarantees above simple to reason about. The threads guide says plainly that the free-threaded build is no faster for fiber throughput.
What 3.14t speeds up is work pyfect hands to other threads:
fx.blocking(f)andfx.try_blockingrun a function on a thread pool. On the GIL build, Python code there takes turns with your run's thread. On 3.14t it runs at the same time. Interrupting the fiber abandons the thread; it can't stop it.fx.on_thread(eff)runs a whole effect as a run of its own on a new thread. On 3.14t, several of them use several cores:
import pyfect as fx
@fx.fn
def checksum(rows: int):
# Many small steps, so it can't be a single fx.blocking call.
total = 0
for i in range(rows):
total += yield from fx.sync(lambda i=i: i * i % 7)
return total
# Each on_thread effect is a run of its own on a new thread: on 3.14t the four
# run on four cores at once; on the GIL build they take turns.
jobs = [fx.on_thread(checksum(20_000)) for _ in range(4)]
print(fx.run_blocking(fx.all(*jobs, concurrency=4)))Unlike fx.blocking, an effect under on_thread can be interrupted: interrupting the caller
interrupts the effect on its own run and waits for its finalizers, so a timeout around it
still cleans up. The parallel effects guide has measurements of when it pays
off and when the cost of a new run outweighs it.
When several runs on different threads share state, pyfect's shared types (Ref, Queue,
Semaphore, Deferred, Pool and others) keep each operation atomic across them. On 3.14t
those threads really do run at the same time, which surfaces races the GIL hides. pyfect's CI
runs a cross-thread stress test and the runtime's test suites on 3.14t for that reason, and the
library's extras are chosen so that none of them turns the GIL back on.
So the honest summary: on 3.14t, structured concurrency in pyfect works the same as on the
regular build, and CPU-heavy work you move to fx.blocking or fx.on_thread can now use more
than one core. On the regular build, the way to use more cores is a worker process.
Where to go next
The concurrency guide covers fiber sets, semaphores, queues and races in more depth. Resources and scopes covers what you can attach cleanup to, and threads and throughput covers what to expect from one run and when to split work across threads or processes.