class asyncio.Future(*, loop=None)
This class is almost compatible with concurrent.futures.Future
.
Differences:
-
result()
andexception()
do not take a timeout argument and raise an exception when the future isn’t done yet. - Callbacks registered with
add_done_callback()
are always called via the event loop’scall_soon_threadsafe()
. - This class is not compatible with the
wait()
andas_completed()
functions in theconcurrent.futures
package.
This class is not thread safe.
-
cancel()
-
Cancel the future and schedule callbacks.
If the future is already done or cancelled, return
False
. Otherwise, change the future’s state to cancelled, schedule the callbacks and returnTrue
.
-
cancelled()
-
Return
True
if the future was cancelled.
-
done()
-
Return True if the future is done.
Done means either that a result / exception are available, or that the future was cancelled.
-
result()
-
Return the result this future represents.
If the future has been cancelled, raises
CancelledError
. If the future’s result isn’t yet available, raisesInvalidStateError
. If the future is done and has an exception set, this exception is raised.
-
exception()
-
Return the exception that was set on this future.
The exception (or
None
if no exception was set) is returned only if the future is done. If the future has been cancelled, raisesCancelledError
. If the future isn’t done yet, raisesInvalidStateError
.
-
add_done_callback(fn)
-
Add a callback to be run when the future becomes done.
The callback is called with a single argument - the future object. If the future is already done when this is called, the callback is scheduled with
call_soon()
.Use functools.partial to pass parameters to the callback. For example,
fut.add_done_callback(functools.partial(print, "Future:", flush=True))
will callprint("Future:", fut, flush=True)
.
-
remove_done_callback(fn)
-
Remove all instances of a callback from the “call when done” list.
Returns the number of callbacks removed.
-
set_result(result)
-
Mark the future done and set its result.
If the future is already done when this method is called, raises
InvalidStateError
.
-
set_exception(exception)
-
Mark the future done and set an exception.
If the future is already done when this method is called, raises
InvalidStateError
.
Please login to continue.