[Python-ideas] awaiting iterables

Piotr Jurkiewicz piotr.jerzy.jurkiewicz at gmail.com
Sat May 2 23:24:58 CEST 2015


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


More information about the Python-ideas mailing list