This code should create a thread which executes sched.scheduler.run()
, and then schedule an event 5 seconds into the future. However, on Python 3.7.4 on macOS Catalina, the scheduled event callback does not run.
Is it expected that you can schedule events on a running sched.scheduler
? I would assume you can since you are allowed to cancel queued events, and there is no exception thrown.
import datetime
import sched
import threading
import time
class Schedule:
def __init__(self):
self.scheduler = sched.scheduler(time.time, time.sleep)
self.thread = threading.Thread(target=self.scheduler.run)
self.thread.start()
def at(self, when, callback, *args, **kwargs):
e = self.scheduler.enterabs(when.timestamp(), 0, callback, args, kwargs)
return e
target = (datetime.datetime.now() + datetime.timedelta(seconds=5))
s = Schedule()
s.at(target, print, 'Ding!')
while True:
print(time.time() - target.timestamp(), s.scheduler.queue)
time.sleep(1)
According to the documentation https://docs.python.org/3/library/sched.html
run
doesn't do the infinite loop thing your expecting. It consumes the queue and without further events it then returns. Ergo, if run executes before 'Ding' is queued 'Ding' will sit in the queue until the end of days