python run function every n seconds code example

Example 1: how to run a function in interval in python

# this makes program sleep in intervals
from time import time, sleep
while True:
    sleep(1 - time() % 1) # run every 1 second... you can change that
	# thing to run

Example 2: run every minute python

from time import time, sleep
while True:
    sleep(60 - time() % 60)
	# thing to run

Example 3: python timer loop

import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc): 
    print("Doing stuff...")
    # do your stuff
    s.enter(60, 1, do_something, (sc,))

s.enter(60, 1, do_something, (s,))
s.run()