Event Loop

noun

say it: “ih-VENT loop” /ɪˈvɛnt lup/

The part of asyncio that runs async code: whenever one task is waiting, it switches to another that is ready, so one thread keeps many tasks moving.

What is the event loop?

The event loop is the part of asyncio that runs async tasks. Whenever one task is waiting, it switches to another that's ready, so one thread keeps many tasks moving.

import asyncio

async def brew(drink, seconds):
    await asyncio.sleep(seconds)
    print(f"{drink} ready")

async def main():
    await asyncio.gather(brew("tea", 2), brew("espresso", 1))

asyncio.run(main())   # espresso ready, then tea ready: 2 seconds total

In a sentence

  • “A time.sleep inside an async route froze the event loop for every request.”
  • “The event loop ran the next request while the first waited on the movie service.”

Level: Master · More Python terms at this level

Updated

In other subjects