Open DontPanicO opened 1 year ago
Yeah, I think this is reasonable. We need to bikeshed on the name of the flag, and I would like to ensure that when the awaiting task (i.e., the TaskGroup
itself) is cancelled, all awaited tasks (i.e., all running tasks that were created by the TaskGroup
) are cancelled. There are tests for this, look for r.cancel()
in test_taskgroups.py, we should probably clone some of these and run them with the new flag as well.
We need to bikeshed on the name of the flag
Yes, it's quite hard to figure out a name that's descriptive enough but reasonably short. May abort_on_child_exception
?
I would like to ensure that when the awaiting task (i.e., the
TaskGroup
itself) is cancelled, all awaited tasks (i.e., all running tasks that were created by theTaskGroup
) are cancelled. There are tests for this, look forr.cancel()
in test_taskgroups.py, we should probably clone some of these and run them with the new flag as well.
In the local clone of my fork I've already defined new tests for the new intended behaviour and I've copied the ones you've pointed out (setting the new flag to prevent child tasks cancellation).
Should I open a PR now or it'd be better to wait for the flag name to have been found?
abort_on_child_exception
still feels too long. Maybe shield_errors=True
(default False)? Or propagate_errors=False
(default True)? Or spread_errors=True
(default False)? (You see a pattern emerge, I'd like a verb and a noun, and the noun should probably be "errors". :-)
I consider shield_errors
(default False
) to be the closest. propagate_errors
may be misleading since exceptions still propagate after other child tasks have been completed.
I'd like to follow this one, if there are no objections to shield_errors
or better proposals.
Maybe postpone_errors
(default False
)?
Oh! defer_errors=True
(default False).
Oh!
defer_errors=True
(default False).
Fine with me. We all agree on defer_errors=True
(default False
)?
Oh! defer_errors=True (default False).
SGTM
So, since there is still more discussion on Discourse, I'd like to get a review from @achimnol, who (I hope) is collecting all the various possible solutions, e.g. Supervisor (also yours) and PersistentTaskGroup; there's a somewhat recent (post-US-PyCon) writeup in Discourse: https://discuss.python.org/t/asyncio-tasks-and-exception-handling-recommended-idioms/23806/9
Feature or enhancement
Add a flag to
asyncio.TaskGroup
to control whether, if a child task crashes, other should be cancelled or not.Pitch
Currently
asyncio.TaskGroup
always cancels all child tasks if one fails. Even if that's the most common use case, I'd like to be able to switch from the current behaviour to prevent child tasks cancellation when one failure occurs and raise anExceptionGroup
with all exceptions raised (only after other tasks completed).async with
block still have to cause child tasks to be canceled.SystemExit
andKeyboardInterrupt
raised in a child task still cancel other tasks.Example usage:
Looking at
asyncio.TaskGroup
source code, it seems that it could be achieved by adding theabort_on_first_exception
(or whatever it should be named) flag to the__init__
method and then modify_on_task_done
as follow:If it's reasonable to have it in
asyncio
, I'll be glad to submit a PR for it.Previous discussion
Post on discuss
Linked PRs