Python Glossary Русский

Python 3.5+Async programmingfunction

asyncio.isfuture()

introspection

Checks whether an object is Future-like: an asyncio.Future, a Task, or an object with a compatible interface.

Syntax

asyncio.isfuture(obj)

The trap: a concurrent.futures.Future from a thread pool is not an asyncio Future, so isfuture() returns False and you cannot await it until wrapped with wrap_future(). A coroutine is not a Future either. The check is duck-typed on the _asyncio_future_blocking attribute rather than isinstance, so it also accepts third-party loops such as uvloop.

Examples

import asyncio
import concurrent.futures
async def main():
    loop = asyncio.get_running_loop()
    task = asyncio.create_task(asyncio.sleep(0.01))
    print(asyncio.isfuture(loop.create_future()), asyncio.isfuture(task))  # → True True
    print(asyncio.isfuture(concurrent.futures.Future()))  # → False — другой Future
    await task
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy