[Python-ideas] awaiting iterables

Guido van Rossum guido at python.org
Sat May 2 23:29:59 CEST 2015


The asyncio package already has this functionality; check out wait() (it
has various options), as_completed(), gather().

On Sat, May 2, 2015 at 2:24 PM, Piotr Jurkiewicz <
piotr.jerzy.jurkiewicz at gmail.com> wrote:

> There are three modes in which you can await multiple coroutines:
> - iterate over results as they become ready
> - await till all are done
> - await till any is done
>
> For example C# has helper functions WhenAll and WhenAny for that:
>
>     await Task.WhenAll(tasks_list);
>     await Task.WhenAny(tasks_list);
>
> I can imagine the set of three functions being exposed to user to control
> waiting for multiple coroutines:
>
> asynctools.as_done()  # returns asynchronous iterator for iterating over
> the results of coroutines as they complete
>
> asynctools.all_done() # returns a future aggregating results from the
> given coroutine objects, which awaited returns list of results (like
> asyncio.gather())
>
> asynctools.any_done() # returns a future, which awaited returns result of
> first completed coroutine
>
> Example:
>
>     from asynctools import as_done, all_done, any_done
>
>     corobj0 = async_sql_query("SELECT...")
>     corobj1 = async_memcached_get("someid")
>     corobj2 = async_http_get("http://python.org")
>
>     # ------------------------------------------------
>
>     # Iterate over results as coroutines complete
>     # using async iterator
>
>     await for result in as_done([corobj0, corobj1, corobj2]):
>         print(result)
>
>     # ------------------------------------------------
>
>     # Await for results of all coroutines
>     # using async iterator
>
>     results = []
>     await for result in as_done([corobj0, corobj1, corobj2]):
>         results.append(result)
>
>     # or using shorthand all_done()
>
>     results = await all_done([corobj0, corobj1, corobj2])
>
>     # ------------------------------------------------
>
>     # Await for a result of first completed coroutine
>     # using async iterator
>
>     await for result in as_done([corobj0, corobj1, corobj2]):
>         first_result = result
>         break
>
>     # or using shorthand any_done()
>
>     first_result = await any_done([corobj0, corobj1, corobj2])
>
> Piotr
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150502/f9f64b2f/attachment.html>


More information about the Python-ideas mailing list