I wonder is there a way to execute a function for a fixed duration in Python?
I guess it could be implemented using a decorator, for example:
def terminate_if_runs_too_long(func):
def new_func(*args, **kwargs):
with <lock which is valid for fixed duration>:
return func(*args, **kwargs)
return new_func
@terminate_if_runs_too_long
def infinite_loop():
'''Imported function, which cannot be modified'''
while True:
pass
However, I am not sure how to implement the resource/lock which would be available for a fixed duration and would raise an exception once the usage duration is exceeded.
I am looking for a solution, which would work using a single thread.