Aren't you able to catch the cancellation error and retrieve the data from
the inner task's .result()?

On Wed, Jul 31, 2019, 8:00 AM Guido van Rossum <gu...@python.org> wrote:

> I wonder if Nathaniel has something to add? Trio has a different approayto
> cancellation. Of course it is also an entirely new library...
>
> On Tue, Jul 30, 2019 at 9:51 PM <aurelien.lambert...@gmail.com> wrote:
>
>> Oh only now it appears in the list ! I thought the post hadn't working,
>> so I posted again :/.
>>
>> I've fixed my "library" (
>> https://github.com/aure-olli/aiokafka/blob/3acb88d6ece4502a78e230b234f47b90b9d30fd5/syncio.py),
>> and the `wrapped_consumer2` function. Now no double await, so no risk of
>> afterward cancellation.
>>
>>     stop_future = asyncio.Future()
>>     async def wrapped_consumer2():
>>         task = asyncio.ensure_future(consume_data())
>>         try:
>>             await asyncio.wait([task, stop_future])
>>         finally:
>>             task.cancel()
>>         if not task.cancelled():
>>             return task.result()
>>         else:
>>             raise RuntimeError('stopped')
>>
>> Or
>>
>>     import syncio
>>
>>     async def wrapped_consumer():
>>         task = syncio.ensure_sync_future(consume_data())
>>         return await task
>>
>>     stop_future = asyncio.Future()
>>     async def wrapped_consumer2():
>>         task = syncio.ensure_sync_future(consume_data())
>>         try:
>>             await syncio.sync_wait([task, stop_future])
>>         finally:
>>             task.cancel()
>>         if not task.cancelled():
>>             return task.result()
>>         else:
>>             raise RuntimeError('stopped')
>>
>> My only concern is that consistent cancellation state is currently almost
>> impossible with futures and tasks. The only two possibilities are either to
>> ignore cancellation (highly counter intuitive to use), or to directly
>> manipulate the coroutine as a generator (basically rewriting asyncio).
>>
>> Could be another library for sure, but the current state of asyncio makes
>> it really hard to reuse it. So it would mean copy-pasting the whole library
>> while changing few lines here and there.
>> _______________________________________________
>> Python-ideas mailing list -- python-ideas@python.org
>> To unsubscribe send an email to python-ideas-le...@python.org
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/4GROXASMFVVRG3UDB4LVMDXOQPU3KH5V/
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
> --
> --Guido (mobile)
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/VMRGZQT6TX5TPEB2OKLLRMREW3SZCXNF/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CPIRQSDIEB2M7B4Q6RQQRA6PI5SFHXKV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to