[issue40672] asyncio.wait_for: process future result produced during cancelation

2020-05-18 Thread Roman Skurikhin


New submission from Roman Skurikhin :

In https://bugs.python.org/issue40607 asyncio.wait_for behavior was changed so 
it propagates exceptions that happened during cancellation. But it still raises 
`TimeoutError` if cancelation ends with some value being returned. In the 
following example value `42` is lost:

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:

1) Return that value from `asyncio.wait_for`.

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 
`CancelledError` and return value 
(https://github.com/grpc/grpc/blob/44fb37c99f2853cc23f04fba15468980d9e28e41/src/python/grpcio/grpc/experimental/aio/_interceptor.py#L328).

It can also be used with some coroutines developed to be wrapped with 
`wait_for`, for example suppose the following equation solving function:

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.


2) Add some warning about the value is thrown away (in debug mode) and document 
it somewhere.

===

I am a newbie here, so sorry if it is wrong to create such "proposal" issues.

--
components: asyncio
messages: 369278
nosy: Roman Skurikhin, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.wait_for: process future result produced during cancelation
type: enhancement
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue40672>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40607] asyncio.wait_for should reraise future exception even if timeout expires

2020-05-12 Thread Roman Skurikhin


New submission from Roman Skurikhin :

In https://bugs.python.org/issue32751 asyncio.wait_for behaviour was changed 
that when we use timeout=... and the timeout expires, it waits until task is 
canceled. However, in some cases inner task can trigger exception while it 
handles cancellation. Check the following code:


import asyncio


async def ignore_cancel_and_raise():
try:
await asyncio.sleep(20)
except asyncio.CancelledError:
raise Exception('Cancellation failed')


async def main():
try:
await asyncio.wait_for(ignore_cancel_and_raise(), timeout=1)
except asyncio.TimeoutError:
print('Timeout')

asyncio.run(main())


It will print "Timeout" and log a warning that "Task exception was never 
retrieved".

I think that in case inner task cancelation fails with some error, 
asyncio.wait_for should reraise it instead of silently losing it.

--
components: asyncio
messages: 368723
nosy: Roman Skurikhin, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.wait_for should reraise future exception even if timeout expires
type: behavior
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue40607>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com