asyncio.gather()

Async

Runs multiple coroutines concurrently and returns their results as a list.

Signature

asyncio.gather(*coros)

Returns

list

Example

import asyncio

async def fetch(url):
    await asyncio.sleep(1)
    return f'data from {url}'

async def main():
    results = await asyncio.gather(
        fetch('/api/1'), fetch('/api/2')
    )
    print(results)

About asyncio.gather()

asyncio.gather is a Python async function with the signature asyncio.gather(*coros). Runs multiple coroutines concurrently and returns their results as a list. It returns a value of type list.

Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The asyncio.gatherfunction is commonly used in data processing, web development, scripting, and automation tasks.

When working with asyncio.gather(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.

Related Functions