๐Ÿ“ Python

asyncio.gather, create_task and TaskGroup: Concurrency in Practice

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
346
๐ŸŒณ
Level
Advanced

Three ways to run multiple coroutines concurrently. Each has its own niche.

asyncio.gather() โ€” the simplest

import asyncio, httpx

async def fetch(client, url):
    r = await client.get(url)
    return r.status_code

async def main():
    urls = ["https://httpbin.org/get"] * 5
    async with httpx.AsyncClient() as client:
        # All 5 requests run in parallel
        results = await asyncio.gather(*[fetch(client, u) for u in urls])
    print(results)  # [200, 200, 200, 200, 200]

asyncio.run(main())

gather() returns results in the same order as the input coroutines.

asyncio.create_task() โ€” when you need control

async def main():
    # Task is created and IMMEDIATELY starts running
    task1 = asyncio.create_task(fetch(client, url1), name="fetch-1")
    task2 = asyncio.create_task(fetch(client, url2), name="fetch-2")

    # Can cancel while waiting
    await asyncio.sleep(0.1)
    task1.cancel()  # changed my mind

    try:
        result = await task2
    except asyncio.CancelledError:
        pass

asyncio.wait() and as_completed() โ€” for complex scenarios

async def main():
    tasks = {asyncio.create_task(fetch(c, u)) for u in urls}

    # Process as they complete โ€” don't wait for the slowest
    async for coro in asyncio.as_completed(tasks):
        result = await coro
        print(f"Done: {result}")

TaskGroup (Python 3.11+) โ€” structured concurrency

async def main():
    results = []
    async with asyncio.TaskGroup() as tg:
        for url in urls:
            tg.create_task(fetch_and_save(url, results))
    # Reach here only when ALL tasks complete
    # If one fails โ€” all others are automatically cancelled
    print(f"Collected: {len(results)}")

TaskGroup is the right choice for new code. It guarantees that when one task fails the others won’t hang forever.

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Event Loop in Python: How asyncio Enables Concurrโ€ฆ

Event loop is the heart of asyncio. It doesn't run code in parallel across multiple...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 386
๐Ÿ“

run_in_executor and anyio: Sync Libraries in Asynโ€ฆ

Sometimes you need to call a synchronous library from async code without blocking the event...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 366
๐Ÿ“

Async Context Managers: async with and @asynccontโ€ฆ

Async context managers manage resources in async code โ€” connections, files, transactions.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 389

Did you like the article?

Subscribe to our updates and be the first to receive new articles. Grow with PyLand!