Method Name
cancel
Method Signature
cancel(event)
Parameters
event – An event that has been already registered with a scheduler instance which needs to be cancelled.
Return Value
None
Exceptions
Raises a ValueError when the event is not found in the internal queue of a scheduler instance.
Overview
- The cancel() method of the scheduler class from the Python Standard Library module sched, removes an already scheduled event from a scheduler instance.
- The cancel() method does not remove an event from the queue once the scheduler is started by calling the run() method.
Example
# Example Python program to cancel an already scheduler
# event with a scheduler instance
import sched
import time
def eventFunction(description):
print("Executing event:%s"%description);
# Create an event scheduler
eventScheduer = sched.scheduler(time.time, time.sleep);
# Add events to the scheduler
e1 = eventScheduer.enterabs(time.time()+5, 1, eventFunction, argument= ("Event1",));
e2 = eventScheduer.enterabs(time.time()+10, 1, eventFunction, argument= ("Event2",));
e3 = eventScheduer.enterabs(time.time()+15, 2, eventFunction, argument= ("Event3",));
# Remove an event before the scheduler starts
eventScheduer.cancel(e1);
eventScheduer.run();
# Remove an event after the scheduler starts...which fails
eventScheduer.cancel(e3);Output
The example Python program removes the e1 from the queue. However, once the scheduler is started it is unable to remove e3.
Executing event:Event2
Executing event:Event3
Traceback (most recent call last):
File "cancelex.py", line 23, in <module>
eventScheduer.cancel(e3);
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sched.py", line 96, in cancel
self._queue.remove(event)
ValueError: list.remove(x): x not in list