Await call should utilize new 3.13 free threaded CPython feature

If you try new free threaded python on multiple cpu cores for something like this:

def compute:
    while True: pass
[threading.Thread(target=compute).start() for _ in range(10)]

You will be happy to notice all your CPU cores being utilized, however here:

async def compute_async():
    while True: pass
await asyncio.gather(*[compute_async() for _ in range(10)])

You will be stuck with one core at a time, unless you do something like asyncio.to_thread. Would it not be great, if awaiting coroutine also just utilizes the freedom of free threaded CPython?

No, this would not be great, since then async would fail to uphold it’s behavioral guarantees. If you want something multi threaded, you need to opt in, and asyncio doesn’t make this hard (as you noticed)

6 Likes

asyncio, as the name suggests, was designed for io-bound tasks (in a single thread), rather than compute-bound tasks (needing multiple threads).