[Python-ideas] Are there asynchronous generators?

Yury Selivanov yselivanov.ml at gmail.com
Mon Jun 29 00:14:20 CEST 2015


On 2015-06-28 11:52 AM, Paul Sokolovsky wrote:
>> There is also the problem that one cannot easily feed a queue,
>> >asynchronous generator, or any asynchronous iterator to a simple
>> >synchronous consumer like sum() or list() or "".join(). It would be
>> >nice if there was a way to wrap them to asynchronous ones when needed
>> >– something like (async sum)(asynchronously_produced_numbers()).
> All that is easily achievable with classical Python coroutines, not
> with asyncio garden variety of coroutines, which lately were casted
> into a language level with async/await disablers:
>
> def coro1():
>      yield 1
>      yield 2
>      yield 3
>
> def coro2():
>      yield from coro1()
>      yield 4
>      yield 5
>
> print(sum(coro2()))


You have easily achieved combining two generators with 'yield from' and 
feeding that to 'sum' builtin.

There is no way to combine synchronous loops with asynchronous 
coroutines; by definition, the entire process will block while you are 
iterating trough them.

Yury


More information about the Python-ideas mailing list