I found a solution for executing a function periodically every N seconds.
import threading;
def do_every (interval, worker_func, iterations = 0):
if iterations != 1:
threading.Timer (
interval,
do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
).start ();
worker_func ();
def print_hw ():
print "hello world";
def print_so ():
print "stackoverflow"
# call print_so every second, 5 times total
do_every (1, print_so, 5);
# call print_hw two times per second, forever
do_every (0.5, print_hw);
When we execute this code it start print "hello world" and "stackoverflow" at the moment execution (at time=0 seconds). What is the possible way to make it printat time=1 second and time=0.5 seconds (not starting at time=0 seconds) without using time.sleep