asyncio.TaskGroup, not gather, for a group of tasks
gather keeps running the other tasks when one raises, and the exception surfaces late. A task group cancels the rest on the first failure and raises them together.
async with asyncio.TaskGroup() as tg:
a = tg.create_task(fetch(url_a))
b = tg.create_task(fetch(url_b))
print(a.result(), b.result())Python 3.11 plus. Structured concurrency: nothing outlives the block it was started in.
pythonconcurrency