Reputation: 128
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?
Upvotes: 2
Views: 613
Reputation: 8258
If you want to use a decorator, here is a working example:
import time
def scheduler(timeout=5, interval=0):
def decorator(function):
def wrapper(*args, **kwargs):
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(interval)
function(*args, **kwargs)
return wrapper
return decorator
@scheduler(timeout=5, interval=1)
def any_function(name):
print(f'hello {name}')
any_function('id3a')
It gives the output:
hello id3a
hello id3a
hello id3a
hello id3a
hello id3a
If you want to create another function to call your function repeatedly, here is an example:
import time
def scheduler(function, timeout=5, interval=0):
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(interval)
function()
def any_function(name):
print(f'hello {name}')
scheduler(lambda: any_function('id3a'), timeout=5, interval=1)
The output is:
hello id3a
hello id3a
hello id3a
hello id3a
hello id3a
Upvotes: 3