class concurrent.futures.Future
Encapsulates the asynchronous execution of a callable. Future
instances are created by Executor.submit()
and should not be created directly except for testing.
-
cancel()
-
Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return
False
, otherwise the call will be cancelled and the method will returnTrue
.
-
cancelled()
-
Return
True
if the call was successfully cancelled.
-
running()
-
Return
True
if the call is currently being executed and cannot be cancelled.
-
done()
-
Return
True
if the call was successfully cancelled or finished running.
-
result(timeout=None)
-
Return the value returned by the call. If the call hasn’t yet completed then this method will wait up to timeout seconds. If the call hasn’t completed in timeout seconds, then a
concurrent.futures.TimeoutError
will be raised. timeout can be an int or float. If timeout is not specified orNone
, there is no limit to the wait time.If the future is cancelled before completing then
CancelledError
will be raised.If the call raised, this method will raise the same exception.
-
exception(timeout=None)
-
Return the exception raised by the call. If the call hasn’t yet completed then this method will wait up to timeout seconds. If the call hasn’t completed in timeout seconds, then a
concurrent.futures.TimeoutError
will be raised. timeout can be an int or float. If timeout is not specified orNone
, there is no limit to the wait time.If the future is cancelled before completing then
CancelledError
will be raised.If the call completed without raising,
None
is returned.
-
add_done_callback(fn)
-
Attaches the callable fn to the future. fn will be called, with the future as its only argument, when the future is cancelled or finishes running.
Added callables are called in the order that they were added and are always called in a thread belonging to the process that added them. If the callable raises an
Exception
subclass, it will be logged and ignored. If the callable raises aBaseException
subclass, the behavior is undefined.If the future has already completed or been cancelled, fn will be called immediately.
The following Future
methods are meant for use in unit tests and Executor
implementations.
-
set_running_or_notify_cancel()
-
This method should only be called by
Executor
implementations before executing the work associated with theFuture
and by unit tests.If the method returns
False
then theFuture
was cancelled, i.e.Future.cancel()
was called and returned True. Any threads waiting on theFuture
completing (i.e. throughas_completed()
orwait()
) will be woken up.If the method returns
True
then theFuture
was not cancelled and has been put in the running state, i.e. calls toFuture.running()
will return True.This method can only be called once and cannot be called after
Future.set_result()
orFuture.set_exception()
have been called.
-
set_result(result)
-
Sets the result of the work associated with the
Future
to result.This method should only be used by
Executor
implementations and unit tests.
-
set_exception(exception)
-
Sets the result of the work associated with the
Future
to theException
exception.This method should only be used by
Executor
implementations and unit tests.
Please login to continue.