I'm calling a function in Python which I know may stall and force me to restart the script.
How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else?
the. vik's answer would be to use the with statement to give the timeout function some syntactic sugar: import signal from contextlib import contextmanager class TimeoutException(Exception): pass @contextmanager def time_limit(seconds): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.
This allows for very readable code which will disable the alaram signal after a successful run (sets signal.alarm(0)) from contextlib import contextmanager import signal import time @contextmanager def timeout(duration): def timeout_handler(signum, frame): raise Exception(f'block timedout after {duration} seconds') ...
Use one process to keep time/check timeout and another process to call this Python function. from multiprocessing import Processdef inc_forever(): print('Starting function inc_forever()...') print(next(counter))def return_zero():
You may use the signal package if you are running on UNIX:
In [1]: import signal # Register an handler for the timeout In [2]: def handler(signum, frame): ...: print("Forever is over!") ...: raise Exception("end of time") ...: # This function *may* run for an indetermined time... In [3]: def loop_forever(): ...: import time ...: while 1: ...: print("sec") ...: time.sleep(1) ...: ...: # Register the signal function handler In [4]: signal.signal(signal.SIGALRM, handler) Out[4]: 0 # Define a timeout for your function In [5]: signal.alarm(10) Out[5]: 0 In [6]: try: ...: loop_forever() ...: except Exception, exc: ...: print(exc) ....: sec sec sec sec sec sec sec sec Forever is over! end of time # Cancel the timer if the function returned before timeout # (ok, mine won't but yours maybe will :) In [7]: signal.alarm(0) Out[7]: 0
10 seconds after the call signal.alarm(10)
, the handler is called. This raises an exception that you can intercept from the regular Python code.
This module doesn't play well with threads (but then, who does?)
Note that since we raise an exception when timeout happens, it may end up caught and ignored inside the function, for example of one such function:
def loop_forever(): while 1: print('sec') try: time.sleep(10) except: continue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With