This module provides a decorator which runs the function in a separate multiprocessing.Process and sends SIGKILL after the specified timeout if the function didn't complete.
- Python 3.7+ (needed for multiprocessing.Process.kill)
- tblib (to pass the traceback from separate process)
pip install kill-timeoutfromkill_timeoutimportkill_timeoutlimit_in_seconds=5@kill_timeout(limit_in_seconds)deflong_running_function(**parameters):
"""Function which makes some computations It could take too long for some parameters. """
...
try:
result=long_running_function(iterations=9001)
print("Function returned: %r"%result)
exceptTimeoutError:
print("Function didn't finished in %d seconds"%limit_in_seconds)
exceptException:
print("Function failed with its internal error! Its original traceback:")
raise