Class as_completed
Return futures in the order in which they complete
Declaration
class as_completed
source linkDocumentation
This returns an iterator that yields the input future objects in the order
in which they complete. Calling ``next`` on the iterator will block until
the next future completes, irrespective of order.
Additionally, you can also add more futures to this object during
computation with the ``.add`` method
Attributes
- futures : Collection of futures
A list of Future objects to be iterated over in the order in which they
complete
- with_results : bool (False)
Whether to wait and include results of futures as well;
in this case `as_completed` yields a tuple of (future, result)
- raise_errors : bool (True)
Whether we should raise when the result of a future raises an exception;
only affects behavior when `with_results=True`.
Examples
>>> x, y, z = client.map(inc, [1, 2, 3]) # doctest: +SKIP
>>> for future in as_completed([x, y, z]): # doctest: +SKIP
... print(future.result()) # doctest: +SKIP
3
2
4
Add more futures during computation
>>> x, y, z = client.map(inc, [1, 2, 3]) # doctest: +SKIP
>>> ac = as_completed([x, y, z]) # doctest: +SKIP
>>> for future in ac: # doctest: +SKIP
... print(future.result()) # doctest: +SKIP
... if random.random() < 0.5: # doctest: +SKIP
... ac.add(c.submit(double, future)) # doctest: +SKIP
4
2
8
3
6
12
24
Optionally wait until the result has been gathered as well
>>> ac = as_completed([x, y, z], with_results=True) # doctest: +SKIP
>>> for future, result in ac: # doctest: +SKIP
... print(result) # doctest: +SKIP
2
4
3
Methods
▷ def __init__(self, futures=None, loop=None, with_results=False, raise_errors=True) ▶ def add(self, future) Add a future to the collection
This future will emit from the iterator once it finishes
▶ def batches(self) Yield all finished futures at once rather than one-by-one
This returns an iterator of lists of futures or lists of
(future, result) tuples rather than individual futures or individual
(future, result) tuples. It will yield these as soon as possible
without waiting.
Examples
>>> for batch in as_completed(futures).batches(): # doctest: +SKIP
... results = client.gather(batch)
... print(results)
[4, 2]
[1, 3, 7]
[5]
[6]
▷ def clear(self) Clear out all submitted futures
@property
def condition(self)
▶ def count(self) Return the number of futures yet to be returned
This includes both the number of futures still computing, as well as
those that are finished, but have not yet been returned from this
iterator.
▷ def has_ready(self) Returns True if there are completed futures available.
▷ def is_empty(self) Returns True if there no completed or computing futures
▶ def next_batch(self, block=True) Get the next batch of completed futures.
Parameters
Returns
- List of futures or (future, result) tuples
Examples
>>> ac = as_completed(futures) # doctest: +SKIP
>>> client.gather(ac.next_batch()) # doctest: +SKIP
[4, 1, 3]
>>> client.gather(ac.next_batch(block=False)) # doctest: +SKIP
[]
▶ def update(self, futures) Add multiple futures to the collection.
The added futures will emit from the iterator once they finish
Reexports