We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Why Are Coroutines Useful?

Coroutines are Python's answer to slow I/O. When a coroutine hits a wait – network, disk, database – the event loop can switch to something else, so the rest of your code keeps running.

Typical I/O you'll await:

  • HTTP calls (pyfetch())
  • File or database reads/writes
  • External devices or browser APIs
from pyodide.http import pyfetch

resp = await pyfetch("/api/boards")
data = await resp.json()

Coroutines help us perform I/O without forcing our entire program to freeze up while we wait for a response.