# -*- coding: utf-8 -*-
from .assertions import isiter
from .concurrent import ConcurrentExecutor
def wait(*coros_or_futures, limit=0, timeout=None, loop=None,
Wait for the Futures and coroutine objects given by the sequence
futures to complete, with optional concurrency limit.
Coroutines will be wrapped in Tasks.
``timeout`` can be used to control the maximum number of seconds to
wait before returning. timeout can be an int or float.
If timeout is not specified or None, there is no limit to the wait time.
If ``return_exceptions`` is True, exceptions in the tasks are treated the
same as successful results, and gathered in the result list; otherwise,
the first raised exception will be immediately propagated to the
``return_when`` indicates when this function should return.
It must be one of the following constants of the concurrent.futures module.
All futures must share the same event loop.
This functions is mostly compatible with Python standard
an iterable collection yielding coroutines functions.
optional concurrency execution limit. Use ``0`` for no limit.
maximum number of seconds to wait before returning.
exceptions in the tasks are treated the same as successful results,
instead of raising them.
indicates when this function should return.
optional event loop to use.
optional variadic argument to pass to the coroutines function.
tuple: Returns two sets of Future: (done, pending).
TypeError: in case of invalid coroutine object.
ValueError: in case of empty set of coroutines or futures.
TimeoutError: if execution takes more than expected.
async def sum(x, y):
return x + y
done, pending = await paco.wait(
[task.result() for task in done]
# => [3, 7]
# Support iterable as first argument for better interoperability
if len(coros_or_futures) == 1 and isiter(coros_or_futures):
coros_or_futures = coros_or_futures
# If no coroutines to schedule, return empty list
# Mimics asyncio behaviour.
if len(coros_or_futures) == 0:
raise ValueError('paco: set of coroutines/futures is empty')
# Create concurrent executor
pool = ConcurrentExecutor(limit=limit, loop=loop,
# Wait until all the tasks finishes
return (yield from pool.run(timeout=timeout,