Courses/Concurrency & Parallelism

Concurrency & Parallelism

Concurrent asyncio

asyncio.gather runs coroutines side by side.

asyncio.gather schedules many coroutines and waits for all of them. Each await is a chance for the event loop to switch.

import asyncio

async def task(name, delay):
    await asyncio.sleep(delay)
    return name

async def main():
    return await asyncio.gather(task("a", 0), task("b", 0), task("c", 0))

print(await main())
main.py
Output
Press Run to execute.
Expected output
item-0
item-1
item-2

Sign in to track your progress across lessons.