-
-
Notifications
You must be signed in to change notification settings - Fork 32k
asyncio.wait_for: process future result produced during cancelation #84849
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
Comments
In https://bugs.python.org/issue40607 asyncio.wait_for behavior was changed so it propagates exceptions that happened during cancellation. But it still raises import asyncio
async def return_42_on_cancel():
try:
await asyncio.sleep(20)
except asyncio.CancelledError:
return 42 # `return` is useless in this block.
async def main():
try:
await asyncio.wait_for(return_42_on_cancel(), timeout=1)
except asyncio.TimeoutError:
print('Timeout')
asyncio.run(main()) I think it's better to either:
The motivation here is that if the task returns something, we shouldn't conceal it. I also searched through GitHub and found some places where others catch It can also be used with some coroutines developed to be wrapped with async def solve_iteratively(initial_x, next_approximation):
result = initial_x
try:
while True:
result = next_approximation(result)
await asyncio.sleep(0)
except asyncio.CancelledError:
return result It allows us to control its execution time using asyncio.wait_for.
=== I am a newbie here, so sorry if it is wrong to create such "proposal" issues. |
The documentation update is definitely something that needs to be done in 3.9. Want to submit a PR? We can also issue a warning in asyncio debug mode. I'm really not sure about "1) Return that value from
The issue is clear, thanks for submitting it! Keep up the great work. |
Regarding the documentation, I'm not sure we _need_ to say what happens in this edge case for 3.9. It was already unspecified before 3.9, so we're not any worse off. (The change in bpo-40607 was, however, documented.) I'd rather come to agreement on (1) first. Because if we document it now, then it could be interpreted as defined behavior and so harder to change later due to backwards compat guarantees. |
Duplicate of #86296 |
Duplicate of #86296 |
(And FWIW I think there we're planning to propagate CancelledError in that case.) |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: