[Python-ideas] PEP 530: Asynchronous Comprehensions

Yury Selivanov yselivanov.ml at gmail.com
Tue Sep 6 13:38:38 EDT 2016


On 2016-09-06 9:40 AM, C Anthony Risinger wrote:
> On Tue, Sep 6, 2016 at 10:20 AM, Sven R. Kunze <srkunze at mail.de 
> <mailto:srkunze at mail.de>> wrote:
>
>     On 06.09.2016 03:16, Yury Selivanov wrote:
>
>
>             Whereas the following will produce some sort of async
>             lists, sets, and dicts?
>
>                     result = [await fun() async for fun in funcs]
>                     result = {await fun() async for fun in funcs}
>                     result = {fun: await fun() async for fun in funcs}
>
>
>             If so, how do I read values from an async list/set/dict?
>
>
>         Consider "funcs" to be an asynchronous generator/iterable that
>         produces a sequence of awaitables.  The above comprehensions
>         will await on each awaitable in funcs, producing regular list,
>         set, and dict.
>
>
>     So, what's the "async" good for then?
>
>
> Maybe I'm off base here, but my understanding is the `async for` 
> version would allow for suspension during the actual iteration, ie. 
> using the __a*__ protocol methods, and not just by awaiting on the 
> produced item itself.
>
> IOW, `[await ... async for ...]` will suspend at least twice, once 
> during iteration using the __a*__ protocols and then again awaiting on 
> the produced item, whereas `[await ... for ...]` will synchronously 
> produce items and then suspend on them. So to use the async + await 
> version, your (async) iterator must return awaitables to satisfy the 
> `async for` part with then produce another awaitable we explicitly 
> `await` on.
>
> Can someone confirm this understanding? And also that all 4 
> combinations are possible, each with different meaning:
>
> # Normal comprehension, 100% synchronous and blocking
> [... for ...]
>
> # Blocking/sync iterator producing awaitables which suspend before 
> producing a "normal" value
> [await ... for ...]
>
> # Non-blocking/async iterator that suspends before producing "normal" 
> values
> [... async for ...]
>
> # Non-blocking/async iterator that suspends before producing 
> awaitables which suspend again before producing a "normal" value
> [await ... async for ...]
>
> Is this correct?

All correct.  I'll update the PEP to better clarify the semantics.

Yury



More information about the Python-ideas mailing list