Tasks, gather & timeouts

P33.event-loop.02 · Audience: guest, it-ml, language-pro · Prerequisites: The event-loop mental model

Real LLM grading for this pageLLM grading (this page):

Time for the real event loop. asyncio.gather runs many coroutines at once and collects their results; asyncio.wait_for bounds one so a hang becomes a clean timeout. Both rungs run on the kernel — real asyncio can't grade in the browser, so these execute in real CPython.

Step 1 / 2gather runs coroutines concurrently
ⓘ Concept: Many awaits, one loop, ordered results
await asyncio.gather(coro1, coro2, ...) schedules every coroutine on the loop, lets them overlap their waiting, and returns a list of results in the order you passed them — not the order they finished. One line, real concurrency.

Why it matters — gather is the workhorse of async I/O: it turns a list of independent awaits into one concurrent batch, without you touching the loop.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

2 graded rungs · ~24 min

Fan out real coroutines with gather, then bound one with wait_for — both graded on the kernel.

Rung 1 — fan out with asyncio.gather (kernel)

Loading exercise…

Rung 2 — timeouts with asyncio.wait_for (kernel)

Loading exercise…

gather runs tasks — but if one fails, the rest leak on. The next track fixes that with structured concurrency: tasks that cannot outlive their block.