Hi Vladimir,

On 2016-08-09 1:24 PM, Vladimir Rutsky wrote:

Hi Yury,

Thank you for posting this PEP!
As an asyncio-based libraries user, author and contributor I
appreciate that topic of easy-writing of asynchronous generators is
being covered now.
I like how simple writing of async generators will be with this PEP,
same with consequent functionality (e.g. async context wrappers).

Thanks a lot for reaching out!

It's great that with this PEP it would be possible to write simple
async data processing wrappers without a knowledge of a single special
method
(__aiter__, __anext__, __aenter__, __aexit__) using same patterns as
regular Python user would use in synchronous code:
[..]

Precisely this.
Can you explain details of canceling of asend and await coroutines?

Consider following example:

async def gen():
     zero = yield 0
     asyncio.sleep(10)
     one = yield 1

async def f(loop):
     g = gen()

     await g.asend(None)

     send_task = loop.create_task(g.asend('zero'))
     send_task.cancel()

     # What is the state of g now?
     # Can I receive one from g? Or cancel() will lead to
CancelledError throw from asyncio.sleep(10)?
     one = g.asend('one')

Will canceling of asend() task result in canceling of generator?


Cancelling `asend()` will result in an asyncio.CancelledError
being raised from the 'await asyncio.sleep(0)' line inside
the async generator:

    async def gen():
        try:
            await asyncio.sleep(1)
        except asyncio.CancelledError:
            print('got it')
            raise  # It's OK to not to re-raise the error too!
        yield 123


    async def run():
        g = gen()
        t = asyncio.ensure_future(g.asend(None))
        await asyncio.sleep(0.5)
        t.cancel()
        await t  # this line will fail with CancelledError

^ the above code will print "got it" and then crash with an
asyncio.CancelledError raised back from 'await t'.

You can handle the error in the generator and continue to iterate
over it.  If you don't catch that exception inside your generator,
the generator will be closed (it's similar to what would happen
if an exception occurs inside a sync generator, or is thrown
into it with gen.throw()).

Thank you,
Yury

_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to