CKTIS / Python
asyncio.gather
Spustí korutiny súbežne a po await poskytne zoznam v poradí argumentov.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
asyncio.gather(*aws, return_exceptions=False)Spustí korutiny súbežne a po await poskytne zoznam v poradí argumentov.
Predvolene odovzdá prvú výnimku a ostatné úlohy pokračujú. Zrušenie čakajúceho gather zruší nedokončených potomkov.
Pri return_exceptions=True obsadí výnimka vlastnú pozíciu, preto sa vypíše 5 a 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())
Príklady
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())
Výsledok
5 ValueError
Pri return_exceptions=True obsadí výnimka vlastnú pozíciu, preto sa vypíše 5 a ValueError.