The event loop schedules ready callbacks and Tasks and reacts to timers or I/O readiness.
Understanding the Concept
The event loop schedules ready callbacks and Tasks and reacts to timers or I/O readiness. A coroutine runs until it awaits, returns or raises.
Blocking operations prevent the event loop from scheduling other tasks promptly.
How the event loop makes progress
When a coroutine reaches await, it may suspend so another ready Task can execute. This scheduling is cooperative: blocking operations that do not yield hold up every coroutine sharing the event loop. Assign ownership of background tasks, put timeouts around operations that may wait, and release resources on cancellation.
An async function does not automatically use several CPU cores. Use async-compatible I/O for waiting tasks and executors or separate processes for blocking or CPU-heavy work when appropriate.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
import asyncio
async def unit(label):
print(label, "before await")
await asyncio.sleep(0)
print(label, "after await")
async def main():
await asyncio.gather(unit("first"), unit("second"))
asyncio.run(main())
Walkthrough: What Each Part Does
- The imports make asyncio available. These are standard-library building blocks unless the example explicitly names an external tool.
- The definition unit, main creates reusable behavior. Its body runs when called, awaited or instantiated; defining it alone does not run the body of an ordinary function.
- Each await is a suspension point. When the awaited operation is not ready, the loop can schedule other tasks instead of blocking the entire thread.
- The coordination call waits for a result or a completion condition. Predict what would happen if the worker failed or never became ready.
- The top-level entry point starts event-loop execution and prints a result after the awaited operation is complete.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
Example Output
first before await
second before await
first after await
second after await
The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.
Important Behavior and Edge Cases
Thinking async means each coroutine gets a dedicated thread or preemption at every Python statement.
Blocking operations prevent the event loop from scheduling other tasks promptly.
Practice Exercise
Predict the order of four print statements and compare it with the actual output.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Thinking async means each coroutine gets a dedicated thread or preemption at every Python statement.
- Calling a blocking function inside the event loop, or creating tasks without awaiting, cancelling or owning them.
Key Takeaways
- The event loop schedules ready callbacks and Tasks and reacts to timers or I/O readiness.
- Blocking operations prevent the event loop from scheduling other tasks promptly.
- Practice: Predict the order of four print statements and compare it with the actual output.