class Monitor(Thread):
"""Thread that calls a monitoring function every ``delay`` seconds."""
def __init__(self, delay, func):
super().__init__(daemon=True)
self.stopped = False
self.delay = delay
self.func = func
self.start_time = time.time()
self.count = 0
def run(self):
while not self.stopped:
time.sleep(0)
self.func()
self.count += 1
def stop(self):
self.stopped = True
# (time.time() - self.start_time) / self.count = 2sec when self.delay == 0.1