Declaring a function async def does not make every operation inside it nonblocking. An asyncio event loop runs its tasks and callbacks in its thread. A synchronous operation that occupies that thread can delay the other tasks. Use an asynchronous API when one exists, or move suitable blocking work to an executor instead of performing it directly in the event-loop thread.
The important question is where control can return
An await expression is part of cooperative scheduling, not an instruction to run arbitrary Python in parallel. A long calculation or a blocking library call can keep control until it finishes. A sleep from the time module blocks its thread; await asyncio.sleep(...) suspends the current task so other scheduled work can proceed. Even a coroutine can run for too long if its execution path does not yield. Identify the operation that is blocking rather than merely looking for async in the surrounding function definition.
Original example: keep the loop responsive
The simulated legacy function below sleeps in a worker thread. Meanwhile, the pulse coroutine can print its ticks. This example uses asyncio.to_thread, available since Python 3.9, and was checked with Python 3.12. It makes no network request and reads no file.
import asyncio
import time
def legacy_read():
time.sleep(0.2) # simulate a blocking library
return "finished"
async def pulse():
for number in range(3):
print("tick", number)
await asyncio.sleep(0.05)
async def main():
result, _ = await asyncio.gather(
asyncio.to_thread(legacy_read), pulse()
)
print(result)
asyncio.run(main())
What the example demonstrates
The tick messages can appear before the blocking function finishes because its sleep occupies a worker thread rather than the event-loop thread. Replacing asyncio.to_thread(legacy_read) with a direct legacy_read() call evaluates that function immediately and blocks before gather can run the coroutines. That is also the wrong argument shape for gather: it expects awaitables, not the returned string. Wrapping the same direct call inside another coroutine still blocks while that wrapper executes. The issue is the operation, not the spelling of the wrapper.
Threads are not a universal CPU-speed solution
On ordinary GIL-enabled CPython builds, to_thread is generally useful for blocking I/O. It does not automatically make CPU-heavy pure-Python work run in parallel. Extensions that release the GIL and other runtime builds can behave differently. A process executor can be appropriate for some CPU-bound work, but brings its own startup, serialization and lifecycle costs. Also check whether the blocking library is safe to call in a worker thread; moving a function does not make shared state thread-safe.
Timeout and cancellation are separate concerns
Canceling the awaiting coroutine does not forcibly stop a synchronous function that is already running in a worker thread. Set the underlying operation’s own timeout where supported, bound outstanding work, and arrange cleanup for the resources it owns. Use asyncio debug mode or slow-callback diagnostics to find stalls, then inspect the actual blocking path. The code above is a scheduling demonstration, not a latency benchmark or a complete production worker-pool policy.
