Package: timeoutd
timeoutdInstallationFromPyPI:pipinstalltimeoutdFrom source code:gitclonehttps://github.com/juhannc/timeoutd.git
pipinstall-e.UsageThetimeoutdmodule provides a decorator that can be used to limit the execution time of a function.
The decorator takes a single argument, the number of seconds or a specific date (as a datetime object) after which the function should be terminated.importtimeimporttimeoutd@timeoutd.timeout(5)defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()Thetimeoutdecorator allows for multiple different ways to specify the timeout, for example with a datetime object:importtimeimportdatetimeimporttimeoutd@timeoutd.timeout(datetime.datetime.now()+datetime.timedelta(0,5))defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()It also take atimedeltaobject:importtimeimportdatetimeimporttimeoutd@timeoutd.timeout(datetime.timedelta(0,5))defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()But it can also take a delta in form of hours, minutes, and seconds via the kwargs:importtimeimporttimeoutd@timeoutd.timeout(hours=0,minutes=0,seconds=5)defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()Thetimeoutdecorator also accepts a custom exception to raise on timeout:importtimeimporttimeoutd@timeoutd.timeout(5,exception_type=StopIteration)defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()You can also specify a function to be called on timeout instead of raising an exception:importtimeimporttimeoutddefadd_two_numbers(i:int,j:int|None=None):ifjisNone:j=0print(f"The sum of{i= }and{j= }is{i+j}")@timeoutd.timeout(5,on_timeout=add_two_numbers,on_timeout_args=(1,),on_timeout_kwargs={"j":2})defmytest():print("Start")foriinrange(1,10):time.sleep(1)print(f"{i}seconds have passed")if__name__=='__main__':mytest()MultithreadingNote:This feature appears to be broken in some cases for the original timeout-decorator.
Some issues might still exist in this fork.By default,timeoutduses signals to limit the execution time of the given function.
This approach does not work if your function is executed not in a main thread (for example if it's a worker thread of the web application).
There is alternative timeout strategy for this case - by using multiprocessing.
To use it, just passuse_signals=Falseto the timeout decorator function:importtimeimporttimeoutd@timeoutd.timeout(5,use_signals=False)defmytest():print"Start"foriinrange(1,10):time.sleep(1)print("{}seconds have passed".format(i))if__name__=='__main__':mytest()Warning:Make sure that in case of multiprocessing strategy for timeout, your function does not return objects which cannot be pickled, otherwise it will fail at marshalling it between master and child processes.AcknowledgementDerived fromhttp://www.saltycrane.com/blog/2010/04/using-python-timeout-decorator-uploading-s3/,https://code.google.com/p/verse-quiz/source/browse/trunk/timeout.py, andhttps://github.com/pnpnpn/timeout-decorator