I need to start the execution of a function foo() every 10 seconds, and the foo() function takes a random time between 1 and 3 seconds to be executed.
import time
import datetime
def foo():
# ... code which takes a random time between 1 and 3 seconds to be executed
while True:
foo()
time.sleep(10)
of course in the above example the function foo() is not executed every 10 seconds but every 10 + random seconds.
Is there a way to start executing foo() each exactly 10 seconds? I'm sure it's something related with threading but can't find a proper example.