Python Forum
how to cancel scheduler module event
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to cancel scheduler module event
#1
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!!

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python script through Task Scheduler? Winfried 8 521 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Confirm / Cancel button in Python & Flask ladak 0 798 Mar-18-2023, 11:26 AM
Last Post: ladak
  Cleanest Way to Cancel a ThreadPool SvetlanaofVodianova 0 1,608 Dec-10-2019, 06:25 PM
Last Post: SvetlanaofVodianova
  Getting an .exe to run from Task Scheduler fioranosnake 2 3,024 Dec-06-2019, 12:20 PM
Last Post: DeaD_EyE
  Simple mutli-threaded scheduler using sched - stuck Mzarour 2 6,148 Nov-12-2019, 07:44 PM
Last Post: Mzarour
  Problem: Once I cancel the process my program will start working! Hadad 0 1,658 Jul-24-2019, 04:09 PM
Last Post: Hadad
  Batch file not running python script in task scheduler davork 3 4,535 May-09-2019, 12:53 PM
Last Post: Gribouillis
  sched.scheduler -> clean denisit 1 2,887 Nov-28-2018, 09:52 AM
Last Post: Gribouillis
  Windows 10 Task Scheduler Error mypython 1 2,499 Aug-11-2018, 11:01 PM
Last Post: ichabod801
  Scheduler runs but then fails marciokoko 2 4,110 Jan-19-2017, 12:02 AM
Last Post: marciokoko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020