[Python-ideas] PEP 525: Asynchronous Generators

Yury Selivanov yselivanov.ml at gmail.com
Sat Aug 6 13:11:00 EDT 2016


On 2016-08-06 1:03 PM, Stefan Behnel wrote:

> Yury Selivanov schrieb am 06.08.2016 um 18:39:
>> You can implement async generators using async iteration protocol as
>> it was defined in PEP 492.
>> [...]
>> Essentially, because pure Python async generators work fine
>> with 'async for' and will continue to do so in 3.6, I think
>> there shouldn't be technical problems to add asynchronous
>> generators in Cython.
> Hmm, I thought that at least interoperability might get in the way. I guess
> I'll just have to give it a try...

Maybe, if we add 'yield from' (which I really don't want to
bother with in 3.6 at least).

It'd be extremely helpful if you could prototype the PEP
in Cython!

>> 2. It's extremely unlikely that somebody will design a system that
>> switches coroutine runners *while async/awaiting a coroutine*.
> Yes, I guess so.
>
>
>> But even in this unlikely use case, you can
>> easily stack finalizers following this pattern:
>>
>>    old_finalizer = sys.get_asyncgen_finalizer()
>>    sys.set_asyncgen_finalizer(my_finalizer)
>>    try:
>>      # do my thing
>>    finally:
>>      sys.set_asyncgen_finalizer(old_finalizer)
> That only works for synchronous code, though, because if this is done in a
> coroutine, it might get suspended within the try block and would leak its
> own finalizer into the outer world.

set_asyncgen_finalizer is designed to be used *only* by coroutine
runners.  This is a low-level API that coroutines should never
touch.  (At least my experience working with coroutines says so...)

Thank you,
Yury


More information about the Python-ideas mailing list