Let's say I want to execute any function repeatedly for 5 seconds. I can do something like this:
def any_function(name):
print(f"hello {name}")
import time
timeout = 5 # [seconds]
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(1) # optional, just to slow down execution
any_function("id3a") #could be any function
what if I want to make this while loop available for other functions, I tried to use a decorator - see below - but it breaks the while loop after the first iteration.
def decorator_function(original_function):
import time
timeout = 5 # [seconds]
timeout_start = time.time()
while time.time() < timeout_start + timeout:
def wrapper_function(*args, **kwargs):
time.sleep(1) # optional, just to slow down execution
return original_function(*args,**kwargs)
return wrapper_function
@decorator_function
def any_function(name):
print(f"hello {name}")
any_function("id3a")
How would you do it?