Event loop and coroutines
coreintermediateThe event loop runs one task at a time, switching to another whenever the current one awaits something. A coroutine is a function defined with async def — calling it builds a paused coroutine object, it does not run yet.
Think of it as
The event loop is a single chef working several dishes at once. It works on one dish, and the moment that dish needs to "wait" (water boiling), the chef switches to another dish instead of standing still. Nothing ever actually overlaps — only the waiting does.
What we're doing: Show that calling a coroutine function does not run it, and that await is what actually runs it.
- 10
- greet("Ada") builds a coroutine object. No print inside greet has run yet.
- 12
- await coro is what actually starts running greet — the two "starting"/"done" prints happen here.
type of coro: coroutine
Ada: starting
Ada: done
result: hello, AdaWhy this works: Defining a coroutine and running it are two separate steps, unlike a normal function call. This separation is what lets asyncio.gather() and create_task() schedule several coroutines before any of them actually starts.
Calling a coroutine function without await and expecting it to run
Wrong
Better
What you see: The program finishes without printing "saved: ..." at all, and Python prints a RuntimeWarning: coroutine 'save_record' was never awaited.
Why: A coroutine call only builds an object describing the work — it is not scheduled or run until something awaits it or wraps it in create_task(). Forgetting await silently skips the work instead of raising an error.
- main() → greet(): coro = greet("Ada") (builds a coroutine object — nothing runs yet)
- main() → greet(): await coro
- greet() → greet(): await asyncio.sleep(0.05) (loop switches to other work here)
- greet() → main(): return "hello, Ada"
asyncio — the entry points worth knowing
Together
Remember: async def call() builds a coroutine object that does nothing; await is what actually runs it and gives back the result.
See also: awaitables tasks and futures · scheduling with gather · asynchronous programming

