Package: timeout-function-decorator
Timeout function decoratorTimeout decorator for synchronous and asynchronous functions.DependenciesWritten in purePythonand has no dependencies other than the base libraries.InstallationFrom source code:pip install .FromPyPI:pip install timeout-function-decoratorImportThere are three ways to import the decorator:Import directly from the packagefromtimeout_function_decoratorimporttimeoutImport directly from the modulefromtimeout_function_decorator.timeout_decoratorimporttimeoutImport the modulefromtimeout_function_decoratorimporttimeout_decoratorThis last case is useful if atimeoutfunction is already present in your namespace.UsageUsing the decorator is as simple as:importtimefromtimeout_function_decoratorimporttimeout@timeout()defi_will_never_time_out(value):whileTrue:time.sleep(1)@timeout(None)defi_will_never_time_out(value):whileTrue:time.sleep(1)@timeout(1)defi_will_not_time_out(value):returnvalue@timeout(1)defi_will_time_out(value):time.sleep(2)returnvalue@timeout(1,RuntimeError)defi_will_raise_runtime_error(value):time.sleep(2)returnvalueAs you may have noticed, the decorator requires the brackets even when no parameters are passed.The same result could be obtained for asynchronous functions:importasynciofromtimeout_function_decoratorimporttimeout@timeout()asyncdefi_will_never_time_out(value):whileTrue:awaitasyncio.sleep(1)@timeout(None)asyncdefi_will_never_time_out(value):whileTrue:awaitasyncio.sleep(1)@timeout(1)asyncdefi_will_not_time_out(value):returnvalue@timeout(1)asyncdefi_will_time_out(value):awaitasyncio.sleep(2)returnvalue@timeout(1,RuntimeError)asyncdefi_will_raise_runtime_error(value):awaitasyncio.sleep(2)returnvalueIf you already have atimeoutfunction in your namespace, you could as easily use the decorator with the module namespace:importtimefromtimeout_function_decoratorimporttimeout_decorator@timeout_decorator.timeout(1)defi_still_time_out(value):time.sleep(2)returnvalue:warning:Warning:When a function times out, an exception is raised but cancellation is not guaranteed. This decorator only notifies the user when enough time has passed since a function call. Handling of the situation and ensuring cancellation is up to the user.SignatureIn general, the decorator accepts two parameters:timeout_duration: afloatspecifying the timeout time in seconds. IfNone, the function will not time out. Defaults toNoneexception_to_raise: theExceptiontype to be raised. Defaults toTimeoutError.They can be passed as positional or keyword arguments.Intended use casesThe wrapper uses asyncio and threads to keep track of timeouts.
This adds a non-trivial overhead on the wrapped functions, in particular on the synchronous ones as they need a thread to be
created.As such, common use cases would include test suites where control on what happens when they time out is important, as with
packages likepytest-timeoutyou can not have tests fail with a specified exception.Use cases in production code, on the other hand, would be limited to long functions with a low call rate as the overhead is
linear with the number of calls but independent of the size of the function or its execution time.A notebook outlining the impact of the overhead on sync and async functions can be foundhere.Plots from the notebook are presented below for a quick performance evaluation.Performance evaluationGeneral performanceSync performanceQuick function performanceMedium function performanceLong function performanceHuge function performanceAsync performanceQuick function performanceMedium function performanceLong function performanceHuge function performance