from threading import Timer
import time
class RepeatTimer(Timer): # Class to have an ever-repeating timer
daemon=True # So that the thread stops if there is a keyboard interrupt
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
def dummyfn(msg="foo"): # Function that executes whenever the timer is called
print(msg)
timer = RepeatTimer(1, dummyfn) # Timer executing the previous function every second
timer.start() # Starting the timer
while 1: # Rest of the code
print("Hello")
time.sleep(0.5)
timer.cancel() # If you want to stop the timer
# This should execute the main program (print "Hello" every 0.5 seconds), and execute
# the timer function every 1 s, no matter what happens in the main program