Future

class Future[source]

Bases: Generic[ResultType]

Future is used for representing an asynchronous computation result.

set_result(result: ResultType) None[source]

Sets the result of the Future.

Parameters

result – Result of the Future.

set_exception(exception: Exception, traceback: Optional[TracebackType] = None) None[source]

Sets the exception for this Future in case of errors.

Parameters
  • exception – Exception to raise in case of error.

  • traceback – Traceback of the exception.

result() ResultType[source]

Returns the result of the Future, which makes the call synchronous if the result has not been computed yet.

Returns

Result of the Future.

is_success() bool[source]

Determines whether the result can be successfully computed or not.

done() bool[source]

Determines whether the result is computed or not.

Returns

True if the result is computed, False otherwise.

running() bool[source]

Determines whether the asynchronous call, the computation is still running or not.

Returns

True if the result is being computed, False otherwise.

exception() Optional[Exception][source]

Returns the exceptional result, if any.

Returns

Exceptional result of this Future.

traceback() Optional[TracebackType][source]

Traceback of the exception.

add_done_callback(callback: Callable[[Future], None]) None[source]
continue_with(continuation_func: Callable[[...], Any], *args: Any) Future[source]

Create a continuation that executes when the Future is completed.

Parameters
  • continuation_func – A function which takes the Future as the only parameter. Return value of the function will be set as the result of the continuation future. If the return value of the function is another Future, it will be chained to the returned Future.

  • *args – Arguments to be passed into continuation_function.

Returns

A new Future which will be completed when the continuation is done.

combine_futures(futures: Sequence[Future]) Future[source]

Combines set of Futures.

It waits for the completion of the all input Futures regardless of their output.

The returned Future completes with the list of the results of the input Futures, respecting the input order.

If one of the input Futures completes exceptionally, the returned Future also completes exceptionally. In case of multiple exceptional completions, the returned Future will be completed with the first exceptional result.

Parameters

futures – List of Futures to be combined.

Returns

Result of the combination.