Finding the call that blocks your asyncio event loop
An async service answers most requests in a few milliseconds, and once in a while everything freezes for a quarter of a second. Nothing is slow on its own, but one coroutine is not yielding, and the event loop cannot switch while it runs.
Let asyncio tell you
Debug mode logs any callback or task step that runs longer than loop.slow_callback_duration, which is 100 ms by default:
import asyncio, time
async def handler():
time.sleep(0.15) # blocking call inside a coroutine
asyncio.run(handler(), debug=True)
The log line names the task and where it was created: Executing <Task ... coro=<handler() ...>> took 0.156 seconds. Set the environment variable PYTHONASYNCIODEBUG=1 to enable it without touching code. Debug mode also adds overhead and extra checks, so use it in staging or on a canary, not on all production traffic.

The usual culprits
time.sleep,requests,urllib, synchronous database drivers, andsubprocess.runinside a coroutine.- File operations on slow storage. Reads from a local SSD are quick, but on a network file system they can take a long time.
- CPU-heavy work: JSON encoding of a huge object, image resizing, regular expressions with catastrophic backtracking, password hashing.
- DNS resolution done through the socket module's blocking functions in library code.
The fixes, from cheapest to heaviest
await asyncio.to_thread(func, *args) runs a blocking function in the default thread pool and is the simplest answer for I/O. Threads do not help CPU-bound Python code much, because of the GIL, so send that to ProcessPoolExecutor through loop.run_in_executor. If a good async library exists for the same job, prefer it, since it avoids the thread hop altogether.
One caveat with threads: the default executor has a limited number of workers, so a burst of blocking calls can queue up. Size it deliberately for services that depend on it.
The thread pool has a default size, and it is not unlimited
asyncio.to_thread and loop.run_in_executor(None, ...) both use the loop's default executor, a ThreadPoolExecutor created lazily the first time it is needed. Its default max_workers is min(32, (os.cpu_count() or 1) + 4), which on a small server can be a surprisingly low number. A burst of blocking calls beyond that queues up behind the ones already running, and from the caller's side that queueing looks identical to the original blocking problem: an await that takes far longer than the work it wraps should.
For a service that regularly offloads blocking calls, create and set your own executor explicitly with a size chosen for the workload, rather than relying on the default:
import asyncio
from concurrent.futures import ThreadPoolExecutor
loop = asyncio.get_running_loop()
loop.set_default_executor(ThreadPoolExecutor(max_workers=64))
Set it once, early, before anything calls to_thread and creates the lazy default. And keep the process-pool option in mind separately: it has its own default size, based on CPU count, which is usually right for CPU-bound work but should still be sized deliberately if the machine runs other CPU-heavy processes alongside it.