[issue33403] asyncio.tasks.wait does not allow to set custom exception when return_when=FIRST_EXCEPTION

Hrvoje Nikšić report at bugs.python.org
Wed May 16 16:49:09 EDT 2018


Hrvoje Nikšić <hniksic at gmail.com> added the comment:

"""
At the moment this can be done but it will cancel all the coroutines with any exception that is raised and at some occasions this may not be desired.
"""

Does wait() really "cancel all the coroutines"? The documentation doesn't mention wait() canceling anything it only returns them in the `pending` set. It is gather() and wait_for() that cancel automatically.

If you want to cancel everything on some exception and not on another, you can easily implement the logic yourself, e.g:

tasks = [asyncio.ensure_future(example(x)) for x in range(20)]
done, pending = await asyncio.wait(tasks, return_when=FIRST_EXCEPTION)
for fut in done:
    try:
        fut.result()
    except CancelException:
        for fut in pending:
            fut.cancel()

----------
nosy: +hniksic

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


More information about the Python-bugs-list mailing list