[Python-ideas] PEP 525: Asynchronous Generators

Yury Selivanov yselivanov.ml at gmail.com
Mon Aug 8 13:06:15 EDT 2016


On 2016-08-08 7:44 AM, Sven R. Kunze wrote:
>> Thing is you can't write one piece of code that will accept any type 
>> of generator (sync or async).
>>
>> * send, throw, close, __next__ methods for sync generators, they are 
>> synchronous.
>>
>> * send, throw, close, __next__ methods for async generators, they 
>> *require* to use 'await' on them. There is no way to make them 
>> "synchronous", because you have awaits in async generators.
>
> There are a lot of things you can do with generators (decorating, 
> adding attributes etc.) and none of them require you to "make them 
> synchronous".

You have to be aware of what you're decorating.  Always.  For instance, 
here's an example of a buggy code:

@functools.lru_cache()
def foo():
     yield 123

I'm really against duck typing here.  I see no point in making the API 
for async generators to look similar to the API of sync generators.  You 
have to provide a solid real-world example of where it might help with 
asynchronous generators to convince me otherwise ;)

>
>> Because of the above, duck-typing simply isn't possible.
>
> See above.
>
>> The prefix is there to make people aware that this is a completely 
>> different API, even though it looks similar. 
>
> Sure. So, what do you think about the separate attribute to make 
> people aware? 

There is a separate attribute already -- __class__.  Plus a couple of 
new functions in inspect module: inspect.isasyncgenfunction and 
inspect.isasyncgen.  And the new types.AsyncGeneratorType for isinstance 
checks.

Thanks,
Yury




More information about the Python-ideas mailing list