Asyncio Core
One event loop on one thread interleaving thousands of coroutines that yield at every await. Interviewers spend the most time here, because this is where candidates block the loop or forget to await.
part ofPythonoverview, primer and where to startread it →on this pageshowhide
explore
- The Event Loop4 questions
- Coroutines and await Semantics4 questions
- Tasks, Futures, and gather4 questions
- Cancellation and Timeouts4 questions
- TaskGroup and Structured Concurrency3 questions
- Fire-and-Forget Task Pitfalls3 questions
- Diagnosing a Stalled Loop4 questions
- Streams and Protocols4 questions
- Graceful Shutdown4 questions
questions
page 2 of 2How does asyncio.run() finalize async generators that are left suspended at shutdown?
basics
~10 sAfter cancelling the remaining tasks, asyncio.run() awaits loop.shutdown_asyncgens(), which calls aclose() on every async generator still suspended at a yield inside that loop, so their finally blocks run before the loop is closed.
What does python -m asyncio ps <pid> print, and when would you reach for it?
basics
~20 sNew in Python 3.14, it attaches to a running Python process by PID and prints a table of its pending asyncio tasks — id, name, coroutine stack and awaiter — with no restart and no change to the program.
What is asyncio's transport and protocol layer, and when would you use it instead of streams?
basics
~20 sTransports own the socket and its write buffer; protocols are your callback object, receiving connection_made, data_received, eof_received and connection_lost. Streams are a coroutine-friendly façade built on that pair. Drop to protocols for lower per-connection overhead and zero-copy reads.
When would you use asyncio.wait with FIRST_COMPLETED instead of asyncio.gather, and what must you clean up afterwards?
basics
~20 sUse asyncio.wait with return_when=asyncio.FIRST_COMPLETED when you only need the earliest answer, such as racing a cache against an authoritative source. It returns done and pending sets, raises nothing itself, and leaves the pending tasks running for you to cancel.
showing 31–34 of 34