May-11-2020, 10:31 PM
Hi,
I am stuck trying to figure out how to cancel an event that was created using the scheduler module. Here are the scheduler docs:
https://docs.python.org/3/library/sched.html
I am not sure how to reference the event?
My code below is 100% working, you can try it out. Press "S" to start the program and "Q" to quit the program. I have not found the right code to put into the cancel() function. I am using the scheduler for a 10 second delay, so if I press start and then press quit immediately after, the program does not exit until the full 10 seconds have passed.
Any help is appreciated!!
I rewrote a few things I think the program is a little easier to understand now. Also I attempted to use this example from stackexchange
But it still does not work. If you press S and then press Q within a few seconds it still takes the full 10 seconds before the program is cancelled.
I am stuck trying to figure out how to cancel an event that was created using the scheduler module. Here are the scheduler docs:
https://docs.python.org/3/library/sched.html
I am not sure how to reference the event?
My code below is 100% working, you can try it out. Press "S" to start the program and "Q" to quit the program. I have not found the right code to put into the cancel() function. I am using the scheduler for a 10 second delay, so if I press start and then press quit immediately after, the program does not exit until the full 10 seconds have passed.
Any help is appreciated!!
import time import sched import threading import keyboard s = sched.scheduler(time.time, time.sleep) class Task: def __init__(self): self._running = False self.firstTime = True def terminate(self): self._running = False #s.cancel(?????????) # attempting to cancel all scheduled events here # how to reference my delayCancellable event?? def pass_func(self): pass def delayCancellable(self, set_time): s.enterabs((time.time()+set_time), 1, self.pass_func) s.run() def run(self): if self._running: print("Start Time: ", time.time()) self.delayCancellable(10) if self._running: print("After 10sec: ", time.time()) c = Task() t = threading.Thread(target=c.run) def startCallback(): print("program started") c._running = True t.start() def cancelCallback(): print("program ended") c.terminate() c.firstTime = False keyboard.add_hotkey('s', callback=startCallback) # keyboard "S" used to start program keyboard.add_hotkey('q', callback=cancelCallback) # keyboard "Q" used to cancel program c.firstTime = True while True: if not c.firstTime: break
I rewrote a few things I think the program is a little easier to understand now. Also I attempted to use this example from stackexchange
But it still does not work. If you press S and then press Q within a few seconds it still takes the full 10 seconds before the program is cancelled.
import time import sched import threading import keyboard class Task: def __init__(self): self.schedule = sched.scheduler(time.time, time.sleep) self._running = False self.firstTime = True def stop(self): self._running = False self.schedule.cancel(self.event) # attempting to cancel all scheduled events here # how to reference my delayCancellable event?? def pass_func(self): pass def delayCancellable(self, set_time): self.event = self.schedule.enterabs((time.time()+set_time), 1, self.pass_func) self.schedule.run() def start(self): if self._running: print("Start Time: ", time.time()) self.delayCancellable(10) if self._running: print("After 10sec: ", time.time()) task = Task() thread = threading.Thread(target=task.start) def startCallback(): print("program started") task._running = True thread.start() def cancelCallback(): print("program ended") task.firstTime = False task.stop() keyboard.add_hotkey('s', callback=startCallback) # keyboard "S" used to start program keyboard.add_hotkey('q', callback=cancelCallback) # keyboard "Q" used to cancel program task.firstTime = True while True: if not task.firstTime: break