Why does asyncio.gather() not cancel pending tasks when one task fails?

3 days ago 12
ARTICLE AD BOX

I'm using asyncio. ga ther () to run multiple coroutines in parallel. When one coroutine raises an exception, I expect the remaining tasks to be cancelled. However, they continue running.

import asyncio async def worker(id): await asyncio.sleep(1) if id == 2: raise ValueError("Failing task") return id async def main(): tasks = [worker(i) for i in range(5)] try: await asyncio.gather(*tasks) except Exception as e: print("Error:", e) asyncio.run(main())
Read Entire Article