I want to use scheduler in a tkinter GUI for a stopwatch. I'm not sure if this is a better idea than using a background thread but I read that you should not stop threads, whereas I wanted to start and stop a function constantly.
I made a short snipped of code in Python 2.7 to test the scheduler and it seems like it starts drifting immediately. I wanted it to increment a counter every second, but by one minute I'm off by two seconds (62 seconds have passed instead of 60 seconds).
Is this machine related? Is there something wrong with my code? Should I use another library?
import sched, time
class Scheduler_Test:
def __init__(self):
self.counter = 0
self.time_increment = 1.0
self.end_time = 0.0
self.s = sched.scheduler(time.time, time.sleep)
self.start_time = time.time()
self.s.enter(self.time_increment, 1, self.do_something, (self.s,))
self.s.run() # run the event scheduler
#Simple test of printing out the computer time (sec) and count
def do_something(self, random_kwarg):
print "Time (sec):",time.time(),", count:", self.counter
self.event = self.s.enter(self.time_increment, 1, self.do_something, (random_kwarg,))
self.counter = self.counter + 1
Test = Scheduler_Test()