Python asyncio may run scheduled callbacks eagerly for 25-35% gains
Andrew Svetlov’s proposal keeps the loop responsive under load while preserving asyncio.sleep(0) as an explicit yield to I/O.
CPython’s asyncio event loop may soon run callbacks scheduled with call_soon in the same iteration instead of waiting for the next trip around the loop, a change Andrew Svetlov says speeds chained call_soon paths by about 38%, Queue ping-pong by 25%, and awaiting a task that does not block on I/O by 33%.
Under the design, newly scheduled callbacks keep running inside the current loop pass until a short time budget (default 100 µs), a callback count limit (default 100), an explicit await asyncio.sleep(0), or loop stop intervenes. Saturated servers that already spend their time on I/O see no change; the win is for bursty chains of pure Python work. Limits are tunable per loop, and zero restores today’s behavior.
The only visible semantic shift is possible reordering of call_soon work relative to I/O callbacks. Svetlov reports that two of more than 3,000 asyncio tests needed the opt-out, and that major libraries including aiohttp, httpx, Starlette, FastAPI, AnyIO, and SQLAlchemy pass unchanged. Uvicorn and websockets, which deliberately use sleep(0) to force pending I/O, remain green when that special case is kept.
That special case drew the main design debate. Mike Shardmind preferred treating sleep(0) as an ordinary yield whose exact scheduling is left to the loop, arguing that documenting a hard “poll I/O now” guarantee is brittle. Guido van Rossum countered that sleep(0) is already the common idiom for “give the loop a turn,” and that breaking callers who rely on it could produce hard-to-debug failures. Svetlov’s checks of top PyPI packages confirmed that relaxing the special case fails tests that intentionally drive I/O; the proposal therefore leaves sleep(0) behavior alone.
A long CPU-bound callback can still hold the loop an extra 100-200 µs in the worst case, the same anti-pattern asyncio already discourages. Independent replay of production traffic showed 8-15% lower p95 latency until the network saturated, with no change in backpressure behavior.