diff --git a/func_timeout/__init__.py b/func_timeout/__init__.py index 097215a..71a431a 100644 --- a/func_timeout/__init__.py +++ b/func_timeout/__init__.py @@ -9,7 +9,7 @@ __version__ = '4.0.0' __version_tuple__ = (4, 0, 0) -__all__ = ('func_timeout', 'set_timeout', 'FunctionTimedOut') +__all__ = ('func_timeout', 'set_timeout', 'set_modifiable_timeout', 'FunctionTimedOut') from .exceptions import FunctionTimedOut -from .dafunc import func_timeout, set_timeout +from .dafunc import func_timeout, set_timeout, set_modifiable_timeout diff --git a/func_timeout/dafunc.py b/func_timeout/dafunc.py index 87ec7e3..71efe9d 100644 --- a/func_timeout/dafunc.py +++ b/func_timeout/dafunc.py @@ -14,6 +14,16 @@ __all__ = ('set_timeout', 'func_timeout') def set_timeout(timeout): + ''' + set_timeout - Wrapper to run a function with a given timeout (max execution time) + + @param timeout - Number of seconds max to allow function to execute + + @throws FunctionTimedOut If time alloted passes without function returning naturally + + @see func_timeout + @see set_modifiable_timeout + ''' def _function_decorator(func): def _function_wrapper(*args, **kwargs): return func_timeout(timeout, func, args=args, kwargs=kwargs) @@ -21,6 +31,29 @@ def set_timeout(timeout): return _function_decorator +def set_modifiable_timeout(timeout): + ''' + set_modifiable_timeout - Wrapper to run a function with a given timeout (max execution time) + which can be overriden by passing "forceTimeout" to the function being decorated + + @param timeout - Default Number of seconds max to allow function to execute + + @throws FunctionTimedOut If time alloted passes without function returning naturally + + @see func_timeout + @see set_timeout + ''' + def _function_decorator(func): + def _function_wrapper(*args, **kwargs): + if 'forceTimeout' in kwargs: + useTimeout = kwargs.pop('forceTimeout') + else: + useTimeout = timeout + + return func_timeout(useTimeout, func, args=args, kwargs=kwargs) + return _function_wrapper + return _function_decorator + def func_timeout(timeout, func, args=(), kwargs=None): ''' func_timeout - Runs the given function for up to #timeout# seconds.