Python Glossary Русский

Python 3.14+Async programmingfunction

asyncio.future_add_to_awaited_by()

call graph

Manually records in the call graph that future is awaited by waiter, for custom synchronisation primitives built on Future.

Syntax

asyncio.future_add_to_awaited_by(future, waiter, /)

When a task awaits a Future, the "who waits for whom" link reaches the call graph on its own. A manual record is needed where waiting bypasses await, as inside gather or shield. Every call must be paired with future_discard_from_awaited_by with the same arguments, or the graph keeps showing a waiter that is long gone. Objects that are not a Future or Task are silently ignored. Added in 3.14.

Examples

# Python 3.14+
import asyncio
async def main():
    fut = asyncio.get_running_loop().create_future()
    me = asyncio.current_task()
    asyncio.future_add_to_awaited_by(fut, me)
    graph = asyncio.capture_call_graph(fut)
    print([g.future is me for g in graph.awaited_by])  # → [True]
    asyncio.future_discard_from_awaited_by(fut, me)  # парный вызов обязателен
    fut.cancel()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy