Python 3.4+Async programmingterm
asyncio.Handle
event loop
The object for a scheduled callback, returned by loop.call_soon() and loop.call_soon_threadsafe(). It lets you cancel the call before it happens.
Syntax
handle = loop.call_soon(callback, *args); handle.cancel()
A Handle is neither a task nor a coroutine: you cannot await it, the callback has no result, and an exception from it is merely logged by the loop. After handle.cancel() the call simply never runs and handle.cancelled() returns True. If you need a result or cancellation with cleanup, use asyncio.create_task() and await instead of call_soon().
Examples
import asyncio
async def main():
loop = asyncio.get_running_loop()
h1 = loop.call_soon(print, 'первый')
h2 = loop.call_soon(print, 'второй')
h2.cancel()
await asyncio.sleep(0.01) # → первый — «второй» не напечатан
print(type(h1).__name__, h2.cancelled()) # → Handle True
asyncio.run(main())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy