Python Asyncio Event Loop — ELI5
Picture a single waiter in a busy restaurant. There are a dozen tables, but only one waiter. How does everyone still get served?
The waiter doesn’t stand at Table 1 watching food cook. Instead, they take Table 1’s order, hand it to the kitchen, and immediately walk to Table 2. When the kitchen rings a bell — “Table 1’s pasta is ready!” — the waiter grabs the plate and delivers it. Then back to the next table that needs attention.
That waiter is the event loop.
Your Python program has one thread doing all the work. When a task says “I’m waiting for something” (a network reply, a file read, a timer), the event loop doesn’t freeze. It switches to another task that’s ready right now. When the waiting thing finishes, the event loop comes back and picks up where it left off.
The magic is that nobody sits idle. The waiter is always moving, and tables get served roughly in order of who’s ready — not who arrived first.
Here’s the catch: if the waiter has to do something heavy, like actually cooking a steak at the table, the whole restaurant stalls. That’s why you keep heavy computation out of async code — the event loop can’t switch away from work that never pauses.
One thing to remember: The event loop is just a clever scheduler. It runs one piece of code at a time but switches so fast between waiting tasks that everything feels simultaneous.
See Also
- Python Actor Model Why treating each piece of your program like a person with their own mailbox makes concurrency way less scary.
- Python Aiocache Caching aiocache remembers expensive answers so your async Python app doesn't waste time asking the same question twice.
- Python Aiofiles Async Io aiofiles lets your async Python program read and write files without freezing — because normal file operations secretly block everything.
- Python Aiohttp Understand Aiohttp through an everyday analogy so Python behavior feels intuitive, not random.
- Python Anyio Portability AnyIO lets your async Python code work with any async library — write once, run on asyncio or Trio without changes.