Advanced·9 min·advanced · concurrency
async / await
async / await
A model for code that waits: network I/O, timers, queues. While one coroutine is waiting, the event loop runs another.
Three keywords
async def— defines a coroutine (a function that returns a coroutine object)await x— pause this coroutine untilxcompletesasyncio.gather(*coros)— run many coroutines concurrently and wait for all
In Pyodide
The browser's event loop is asyncio's event loop. await at the top level Just Works.
Common pitfall
Calling async_fn() without await returns a coroutine object — it does not run. Always await or pass to gather.
Try it
- Convert the three
fetch_thingcalls to run sequentially withawait— observe the time difference. - Use
asyncio.create_taskand check the difference vsgather.