-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TaskGroup(wait=all)
, weird behaviour and API of completed
#43
Comments
I believe the intended use is something like this: https://github.com/kyuupichan/electrumx/blob/master/electrumx/server/session.py#L615-L617 At least that is what I use and what I gleaned from https://curio.readthedocs.io/en/latest/ |
Hmm... that means the So I guess I could achieve what example2 wants with something like this: class OldTaskGroup(TaskGroup):
"""Automatically raises exceptions on join; as in aiorpcx prior to version 0.20"""
async def join(self):
if self._wait is all:
try:
async for task in self:
if not task.cancelled():
task.result()
finally:
await super().join()
else:
await super().join()
if self.completed:
self.completed.result() I think |
I've tweaked it a bit more, atm using: class OldTaskGroup(aiorpcx.TaskGroup):
"""Automatically raises exceptions on join; as in aiorpcx prior to version 0.20"""
async def join(self):
if self._wait is all:
exc = False
try:
async for task in self:
if not task.cancelled():
task.result()
except BaseException: # including asyncio.CancelledError
exc = True
raise
finally:
if exc:
await self.cancel_remaining()
await super().join()
else:
await super().join()
if self.completed:
self.completed.result() |
(re git tag
0.22.1
)Using TaskGroups, with the default
wait=all
,Looks like
taskgroup.completed
is a reference to the first task that completes, even if it completes gracefully and does not stop the task group. If a later task then raises an exception, there does not seem to be a way to get a reference for it.taskgroup.exception
will not get set.So my question is: how do I get a reference to the first exception found by the join?
(One could of course iterate
taskgroup.exceptions
but there might be multiple non-cancellation exceptions there in arbitrary order.)Example1:
In particular, ideally something like this would work and exceptions get propagated:
Example2:
The text was updated successfully, but these errors were encountered: