Issue
If I schedule a callback using loop.call_later()
(or with loop.call_at()
, which behaves the same way), is it possible to cancel its execution?
Why? Let's say I schedule something to run one minute from now. However, due to some condition, the code decides to abort that execution (because it's not needed anymore), or alternatively decides to reschedule it for another time. The question is how to implement it using Python's asyncio
?
If you are familiar with JavaScript, I'm looking for equivalents of setTimeout()
and clearTimeout()
.
Solution
From the docs you linked:
An instance of
asyncio.TimerHandle
is returned which can be used to cancel the callback.
And from the docs of asyncio.Handle
, a superclass of asyncio.TimerHandle
:
cancel()
Cancel the callback. If the callback has already been canceled or executed, this method has no effect.
So just call the handle's cancel
method.
Answered By - user2357112 supports Monica
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.