All posts
Article · 1 min ·

Asyncio: The Event Loop

How can Python handle 10,000 requests with one thread? Magic? No. The Event Loop.

The Chess Master Analogy

Imagine a Chess Master playing against 100 people simultaneously.
* He walks to Board 1, makes a move. (CPU work)
* The opponent thinks. (I/O wait)
* He walks to Board 2, makes a move.

He doesn't wait for Opponent 1 to finish thinking. He stays busy.
This is Asyncio.

Blocking the Loop

If the Chess Master stops at Board 1 and calculates Pi to a billion decimals... nobody else gets to play. The game freezes.

In Python:

async def bad_view():
    time.sleep(10) # STOPS THE WORLD

Never block the loop. Use await asyncio.sleep(10).

Conclusion

Asyncio is cooperative multitasking. It relies on you, the developer, to be a good citizen and yield control when waiting.

Related posts