On this page
asyncio.Future
Prev: asyncio.FrameCallGraphEntry | Up: asyncio | Next: asyncio.future_add_to_awaited_by
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.Future
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import Future
Signature & Overview
asyncio.Future(*, loop=None)
This class is almost compatible with concurrent.futures.Future.
Methods & Attributes
Future.add_done_callback(...)— Add a callback to be run when the future becomes done.Future.cancel(self, /, msg=None)— Cancel the future and schedule callbacks.Future.cancelled(self, /)— Return True if the future was cancelled.Future.done(self, /)— Return True if the future is done.Future.exception(self, /)— Return the exception that was set on this future.Future.get_loop(self, /)— Return the event loop the Future is bound to.Future.remove_done_callback(self, fn, /)— Remove all instances of a callback from the "call when done" list.Future.result(self, /)— Return the result this future represents.Future.set_exception(self, exception, /)— Mark the future done and set an exception.Future.set_result(self, result, /)— Mark the future done and set its result.
Usage
import asyncio
async def main():
loop = asyncio.get_running_loop()
fut = loop.create_future()
loop.call_soon(fut.set_result, "resolved")
assert await fut == "resolved"
asyncio.run(main())
Gotchas
- Create with
loop.create_future(), notasyncio.Future(): Ensures the future is bound to the running loop. - Check
not fut.done()beforeset_result(): Setting a result on an already-cancelled or finished future raisesInvalidStateError.
REPL Inspect
from asyncio import Future
help(Future)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/FrameCallGraphEntry Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/future_add_to_awaited_by Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib