[issue31452] asyncio.gather does not cancel tasks if one fails

Andrew Lytvyn report at bugs.python.org
Wed Sep 13 10:53:37 EDT 2017


New submission from Andrew Lytvyn:

If you do not await gather, then if one of gather inner coroutines fails, others keep working, but they should not.


```python
import asyncio
import logging

logging.basicConfig(level=logging.DEBUG)

loop = asyncio.get_event_loop()

async def success_coro(seconds):
    await asyncio.sleep(seconds)
    print(seconds)


async def failed_coro(seconds):
    await asyncio.sleep(seconds)
    print(seconds)
    raise ZeroDivisionError

coros = [
    success_coro(2),
    failed_coro(3),
    success_coro(5),
]

async def waiter():
    await asyncio.gather(*coros)

asyncio.ensure_future(waiter())

loop.run_forever()
```
-------------------------------------------------------------
Console:
2
3
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<waiter() done, defined at tst.py:72> exception=ZeroDivisionError()>
Traceback (most recent call last):
  File "tst.py", line 73, in waiter
    await asyncio.gather(*coros)
  File "tst.py", line 64, in failed_coro
    raise ZeroDivisionError
ZeroDivisionError
5
-------------------------------------------------------------

Expected behavior that 5 should not be printed.

----------
components: asyncio
messages: 302079
nosy: Andrew Lytvyn, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.gather does not cancel tasks if one fails
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31452>
_______________________________________


More information about the Python-bugs-list mailing list