- func_set_timeout(timeout, allowOverride=False)
- func_set_timeout - Decorator to run a function with a given/calculated timeout (max execution time).
Optionally (if #allowOverride is True), adds a paramater, "forceTimeout", to the
function which, if provided, will override the default timeout for that invocation.
If #timeout is provided as a lambda/function, it will be called
prior to each invocation of the decorated function to calculate the timeout to be used
for that call, based on the arguments passed to the decorated function.
For example, you may have a "processData" function whose execution time
depends on the number of "data" elements, so you may want a million elements to have a
much higher timeout than seven elements.)
If #allowOverride is True AND a kwarg of "forceTimeout" is passed to the wrapped function, that timeout
will be used for that single call.
@param timeout <float OR lambda/function> -
Default number of seconds max to allow function to execute
before throwing FunctionTimedOut
If a function/lambda is provided, it will be called for every
invocation of the decorated function (unless #allowOverride=True and "forceTimeout" was passed)
to determine the timeout to use based on the arguments to the decorated function.
The arguments as passed into the decorated function will be passed to this function.
They either must match exactly to what the decorated function has, OR
if you prefer to get the *args (list of ordered args) and **kwargs ( key : value keyword args form),
define your calculate function like:
def calculateTimeout(*args, **kwargs):
or lambda like:
calculateTimeout = lambda *args, **kwargs : ...
otherwise the args to your calculate function should match exactly the decorated function.
@param allowOverride <bool> Default False, if True adds a keyword argument to the decorated function,
"forceTimeout" which, if provided, will override the #timeout. If #timeout was provided as a lambda / function, it
will not be called.
@throws FunctionTimedOut If time alloted passes without function returning naturally
- func_timeout(timeout, func, args=(), kwargs=None)
- func_timeout - Runs the given function for up to #timeout# seconds.
Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises FunctionTimedOut
@param timeout <float> - Maximum number of seconds to run #func# before terminating
@param func <function> - The function to call
@param args <tuple> - Any ordered arguments to pass to the function
@param kwargs <dict/None> - Keyword arguments to pass to the function.
@raises - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised
If the timeout is exceeded, FunctionTimedOut will be raised within the context of the called function every two seconds until it terminates,
but will not block the calling thread (a new thread will be created to perform the join). If possible, you should try/except FunctionTimedOut
to return cleanly, but in most cases it will 'just work'.
@return - The return value that #func# gives