Python Glossary Русский

Python 3.14+Async programmingfunction

asyncio.future_discard_from_awaited_by()

call graph

Removes the call-graph record that future is awaited by waiter; the counterpart of future_add_to_awaited_by().

Syntax

asyncio.future_discard_from_awaited_by(future, waiter, /)

Call it in a finally block so the link is dropped even when the waiter is cancelled or fails; otherwise debugging output shows a task that no longer waits for anything. Removing a missing link is not an error, like set.discard rather than set.remove. For objects that are not a Future or Task it does nothing. 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)
    try:
        pass  # здесь было бы ожидание в обход await
    finally:
        asyncio.future_discard_from_awaited_by(fut, me)
    print(asyncio.capture_call_graph(fut).awaited_by)  # → ()
    asyncio.future_discard_from_awaited_by(fut, me)  # повтор — не ошибка
    fut.cancel()
asyncio.run(main())

See also

Open in the full glossarydocs.python.orgReport an inaccuracy