Four Ways to Do Many Things at Once II: Asyncio and Free-Threaded Python

Updated: Oct 18
In Part I we found that threads are good at waiting and bad at computing, and that processes are the reverse, with a serialisation tax. We also found that concurrent.futures lets us swap between them in one word.
This part adds the other two ways: the different execution model of asyncio, and free-threaded Python with its different rules for threads.
As before, we are looking at what each tool is for and where it stops working, rather than benchmarking them.

Asyncio: One Thread, Many Waiters
Threads are managed by the operating system. It can pause any thread at any moment, which is why shared state gets dangerous.
asyncio flips this around. Everything runs on one thread, inside an event loop. Tasks are cooperative: a task runs until it hits an await, and only then does the loop switch to another task.
Without preemption, tasks avoid surprise interruptions, and most code needs no locks.
The price is that every library in the chain must speak async. Unfortunatly for us, requests does not, so we switch to httpx, which offers both flavours.
The semaphore is the new max_workers. With threads, the pool size limited concurrency for free. With asyncio you could launch 10,000 tasks at once, so you must cap them yourself.
TaskGroup (since Python 3.11) is the modern replacement for a bare asyncio.gather. If one task fails, the others are cancelled, and the error surfaces cleanly instead of leaking background tasks.
await is a promise. Each await says: “I’m waiting now, someone else can go.” Forget it, and the call returns a coroutine object instead of a result.
On my machine this fetches all 50 pages in roughly 3 seconds, with a single thread and noticeably less memory than 50 threads would need.
The memory saving matters here. A thread costs an operating-system stack. A task costs a few kilobytes. This is why asyncio scales to tens of thousands of connections, and threads do not.
Asyncio for CPU Work: The Frozen Loop
Here is the cooperative model’s weak spot. A task that never awaits never yields.
Let’s make it visible with a heartbeat:
Adding in the different functions from the example of Part I and run this. The ticks come steadily for one second, then they stop. For as long as parse is grinding through the pages, the entire event loop is frozen. No timers fire. No other requests progress. A web server built this way would stop answering everyone while one request computes.
We can keep the event loop free by handing the CPU work to a concurrent.futures executor:
run_in_executor is the bridge. It submits work to any concurrent.futures executor and gives back an awaitable. The event loop stays free while a worker process does the computing.
So asyncio handles the thousands of waiting tasks, and processes handle the computing. The same two-pool shape from Part I, with the I/O pool replaced by an event loop.
For blocking libraries that you cannot rewrite, there is also to_thread:
to_thread is the escape hatch when one legacy call would otherwise block the loop. Handy, but if you find yourself wrapping everything with it, you have rebuilt a thread pool with extra syntax.
Free-Threaded Python: Changing the Rules
So far we have worked around the GIL. Free-threaded Python removes it.
It arrived as an experimental build in 3.13, and in 3.14 it is officially supported, though still a separate build you must opt into. You will typically see it called python3.14t, where the t stands for threaded.
You can check which interpreter you have:
On a free-threaded build, something similar to the “proof by failure” from Part I behaves differently, with the very same code:
The threads now run Python bytecode on several cores at once, without a process pool or pickling. The result lands close to the multi-process timing, with all four workers sharing one address space.
CPU-bound code can now use threads, and large shared data no longer needs to be copied between workers.
Some familiar behaviour stays the same, though.
The API is identical. ThreadPoolExecutor is still ThreadPoolExecutor. Free-threading keeps the existing programming model and removes its biggest limitation.
I/O code gets no faster. Threads waiting on a socket were already running concurrently. Our 50-page fetch finishes in about the same time.
Single-threaded code gets slightly slower. The interpreter now needs finer-grained safety mechanisms, so there is a modest overhead. It has been shrinking release by release, but it is not zero.
asyncio is unaffected. One thread is one thread. The event loop does not become parallel.
The Bug That Was Hiding
The catch is that the GIL was quietly protecting a lot of sloppy code. Consider:
counter += 1 is really three steps: read, add, write. Two threads can interleave between those steps and lose an update. If we run this with ordinary Python, the final answer is the expected 4,000,000. If we run it with free-threaded Python, the answer will come up short.
In other words, on a GIL build this bug exists in theory, but thread switches are infrequent enough that it often seems fine in practice. Without the GIL, threads overlap, and the final number comes up short much more reliably.
The fix has always been the same:
Free-threading exposes an existing class of bug by removing the thing that hid it. If your threaded code worked “by accident” for years, a free-threaded build might quickly break it.
The Extension Problem
The second catch is the C ecosystem. Libraries like NumPy, pandas and many others ship compiled extensions. Those extensions were written assuming the GIL would protect their internal state.
An extension has to be explicitly updated and marked as supporting free-threading. If you import one that has not declared support, the interpreter re-enables the GIL for the whole process, usually with a warning. Your free-threaded build silently becomes a normal one.
You can force the issue:
PYTHON_GIL=0 python3.14t my_script.py
or:
python3.14t -X gil=0 my_script.pyBut that tells the interpreter to trust extensions that never promised to be safe. Crashes and corrupted data are a plausible outcome, so this is for experiments, not production.
Support is improving quickly, and the community tracks it at py-free-threading.github.io. Check your actual dependency list there before planning anything around it. That support list can change faster than this post.
When NOT to Use Them
Asyncio for CPU work. We watched the heartbeat die. If a function does not await, it blocks everyone. Offload it with run_in_executor or reconsider the architecture.
Asyncio when your libraries are blocking. If the core of your work is a sync-only client, you will end up wrapping it in to_thread calls. At that point plain threads are simpler and no slower.
Free-threading with incompatible extensions. If a key dependency re-enables the GIL, you get the overhead of the new build and none of the benefit.
Free-threading with unsynchronised shared state. The counter above. Audit before you switch.
Free-threading because it is new. If your pipeline is I/O-bound, or already runs happily on processes, there is nothing to win.
Where This Shows Up in Real Code
These choices show up in some familiar places:
Browser automation. In the Playwright and Selenium posts, the browser does the heavy lifting in a separate process. Python mostly waits, which makes this I/O work. Playwright’s async API pairs naturally with asyncio, while its sync API objects are tied to the thread that created them, so sharing one across a thread pool needs care.
The weather API series. Fetching forecasts for many cities is a classic fan-out. A semaphore or the rate-limiter decorator keeps you on the right side of the provider’s limits, whichever concurrency model sits underneath.
FastAPI. An async def endpoint runs directly on the event loop, so one blocking call freezes the server. A plain def endpoint is automatically run in a thread pool. Knowing which you wrote is half of debugging FastAPI performance.
Ray and Dask. Everything in this post lives on one machine. When four ways are not enough, the engines post is where the story continues: the same ideas, spread across a cluster.
The Whole Map
Workload | Reach for | Watch out for |
A few dozen waiting tasks | Threads | Shared mutable state |
Thousands of waiting tasks | asyncio | Blocking calls, CPU work |
Heavy computation | Processes | Pickling cost, small tasks |
Heavy computation, shared data | Free-threaded threads | Extensions, hidden races |
Mixed pipeline | One pool of each, or asyncio plus an executor | Over-engineering |
We can now be more precise about the sentence from the start of Part I:
Python can’t do parallel computation in threads on a standard build. It can with processes. It can with threads on a free-threaded build, if your libraries cooperate. And for waiting, it never needed parallelism at all.
Decide what your task is waiting for and what it is computing, and the choice mostly makes itself. The concurrent.futures interface will be there whichever way you go.



Comments