[Python-ideas] gevent-like Coroutines in Python

Chris Angelico rosuav at gmail.com
Tue Oct 30 06:40:41 EDT 2018


On Tue, Oct 30, 2018 at 6:01 PM Ron Reiter <ron.reiter at gmail.com> wrote:
>
> ... most developers would always mean they prefer to do this:
>
> result = [await fun(x) for fun in funcs]
>
> versus:
>
> result = [fun(x) for fun in funcs]
> await asyncio.gather(*result)
>
> Moreso, having it become the default makes statements like this:
>
> result = [await fun(x) for fun in funcs if await smth]
>
> Look like this:
>
> result = [fun(x) for fun in funcs if smth]
>
> Therefore, my suggestion is to create a new "async" definition which basically turns every function invocation into an "await" if a generator is returned.
>

I'm not sure what you're driving at here. From your first example, I
gather that (pun intended) you're expecting the 'result' list to
contain all the results from the different function calls, running
them all in parallel; but your second example and described suggestion
seem to imply that the waitings would continue to be sequential.

Unless you're asking for straight-up magic ("do everything in parallel
unless they need to be serialized"), there still needs to be a clear
way to differentiate between "wait for this right now and give me a
result before this function continues" and "gather all these jobs
together, get me the results, and then move on once you have them
all". It might perhaps be nice to have an easier/more obvious syntax
for gather(), but it definitely needs to have some form of spelling.

If you're not asking for them to be run in parallel, you're asking for
an implicit way for a function call to block its caller, and for the
calling function to act sequentially. Python already has that - it's
called threading :)

ChrisA


More information about the Python-ideas mailing list