CKTIS / Python
asyncio.gather
Zažene korutine sočasno in po await poda seznam v vrstnem redu argumentov.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
asyncio.gather(*aws, return_exceptions=False)Zažene korutine sočasno in po await poda seznam v vrstnem redu argumentov.
Privzeto prenese prvo izjemo, preostala opravila pa nadaljujejo. Preklic čakajočega gather prekliče nedokončane otroke.
Pri return_exceptions=True izjema zasede svoj položaj, zato se izpišeta 5 in 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())
Primeri
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())
Rezultat
5 ValueError
Pri return_exceptions=True izjema zasede svoj položaj, zato se izpišeta 5 in ValueError.