Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2018-05-02.00:33:54.589>labels= ['type-feature', '3.8', 'expert-asyncio']
title='asyncio.tasks.wait does not allow to set custom exception when return_when=FIRST_EXCEPTION'updated_at=<Date2018-05-17.16:36:46.486>user='https://github.com/pyneda'
A possible use case (that at least I couldn't find how to solve) is the possibility to cancel a bunch of futures/coroutine objects which are being awaited using asyncio.tasks.wait() by one of the running coroutines when a task succeded or a specific condition happens by just raising an specific exception.
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.
A simple example:
asyncdefexample(num):
ifx==5:
raiseException('Exception that does not cancel')
elifx==15:
raiseCancelException()
tasks= [asyncio.ensure_future(example(x)) forxinrange(20)]
done, pending=awaitasyncio.wait(tasks, return_when=FIRST_EXCEPTION, return_on=CancelException)
This wouldn't cancel everything when a normal exception is raised, instead it will when the exception raised is the one that the user expects to be raised in order to cancel everything that is pending.
In addition, if the user does not specify the Exception type, it uses default Exception so it would keep working exactly as now.
hniksic commented on May 16, 2018
hniksicmannequin
Mannequin
More actions
"""
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:
Activity
pyneda commented on May 16, 2018
A possible use case (that at least I couldn't find how to solve) is the possibility to cancel a bunch of futures/coroutine objects which are being awaited using asyncio.tasks.wait() by one of the running coroutines when a task succeded or a specific condition happens by just raising an specific exception.
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.
A simple example:
This wouldn't cancel everything when a normal exception is raised, instead it will when the exception raised is the one that the user expects to be raised in order to cancel everything that is pending.
In addition, if the user does not specify the Exception type, it uses default Exception so it would keep working exactly as now.
hniksic commented on May 16, 2018
"""
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:
1st1 commented on May 17, 2018
This isn't a priority now, so let's postpone the discussion on this until 3.7 is released.
asvetlov commented on Nov 13, 2024
Thanks for the proposal, but I'm closing the issue.
asyncio develops in a slightly different way; task.cancel() accepts an optional msg parameter that could be utilized by user's code.