<div dir="ltr"><div>The asyncio package already has this functionality; check out wait() (it has various options), as_completed(), gather().<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 2, 2015 at 2:24 PM, Piotr Jurkiewicz <span dir="ltr"><<a href="mailto:piotr.jerzy.jurkiewicz@gmail.com" target="_blank">piotr.jerzy.jurkiewicz@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There are three modes in which you can await multiple coroutines:<br>
- iterate over results as they become ready<br>
- await till all are done<br>
- await till any is done<br>
<br>
For example C# has helper functions WhenAll and WhenAny for that:<br>
<br>
await Task.WhenAll(tasks_list);<br>
await Task.WhenAny(tasks_list);<br>
<br>
I can imagine the set of three functions being exposed to user to control waiting for multiple coroutines:<br>
<br>
asynctools.as_done() # returns asynchronous iterator for iterating over the results of coroutines as they complete<br>
<br>
asynctools.all_done() # returns a future aggregating results from the given coroutine objects, which awaited returns list of results (like asyncio.gather())<br>
<br>
asynctools.any_done() # returns a future, which awaited returns result of first completed coroutine<br>
<br>
Example:<br>
<br>
from asynctools import as_done, all_done, any_done<br>
<br>
corobj0 = async_sql_query("SELECT...")<br>
corobj1 = async_memcached_get("someid")<br>
corobj2 = async_http_get("<a href="http://python.org" target="_blank">http://python.org</a>")<br>
<br>
# ------------------------------------------------<br>
<br>
# Iterate over results as coroutines complete<br>
# using async iterator<br>
<br>
await for result in as_done([corobj0, corobj1, corobj2]):<br>
print(result)<br>
<br>
# ------------------------------------------------<br>
<br>
# Await for results of all coroutines<br>
# using async iterator<br>
<br>
results = []<br>
await for result in as_done([corobj0, corobj1, corobj2]):<br>
results.append(result)<br>
<br>
# or using shorthand all_done()<br>
<br>
results = await all_done([corobj0, corobj1, corobj2])<br>
<br>
# ------------------------------------------------<br>
<br>
# Await for a result of first completed coroutine<br>
# using async iterator<br>
<br>
await for result in as_done([corobj0, corobj1, corobj2]):<br>
first_result = result<br>
break<br>
<br>
# or using shorthand any_done()<br>
<br>
first_result = await any_done([corobj0, corobj1, corobj2])<span class="HOEnZb"><font color="#888888"><br>
<br>
Piotr</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>