CKTIS / Python
asyncio.gather
Runs awaitables concurrently and collects their results in input order.
Version: Python 3.14; repository image, production runtime not attested
Syntax
asyncio.gather(*aws, return_exceptions=False)Runs awaitables concurrently and collects their results in input order.
The result order follows the arguments even when a later argument completes first. The option for collecting exceptions returns exception objects in corresponding positions.
Schedule coroutine arguments concurrently and await one list ordered like the inputs. By default the first exception propagates and siblings continue; cancelling a pending gather instead cancels unfinished children. return_exceptions=True stores exception objects at their input positions. Thus the sample retains 5 before ValueError.
import asyncio
async def main():
gate = asyncio.Event()
slow = asyncio.create_task(gate.wait())
async def fail():
raise ValueError("bad")
try:
await asyncio.gather(slow, fail())
except ValueError:
assert not slow.done()
gate.set()
assert await slow is True
asyncio.run(main())
Examples
import asyncio
async def fail():
raise ValueError("sample")
async def main():
results = await asyncio.gather(asyncio.sleep(0, result=5), fail(), return_exceptions=True)
print(results[0], type(results[1]).__name__)
asyncio.run(main())
Result
5 ValueError
Schedule coroutine arguments concurrently and await one list ordered like the inputs. By default the first exception propagates and siblings continue; cancelling a pending gather instead cancels unfinished children. return_exceptions=True stores exception objects at their input positions. Thus the sample retains 5 before ValueError.