From f8ec7f492c4eff3cd2322cb40e949d8d87c6262f Mon Sep 17 00:00:00 2001 From: Tim Savannah Date: Fri, 19 May 2017 20:49:41 -0400 Subject: [PATCH] Add 'set_timeout' decorator for functions --- func_timeout/__init__.py | 4 ++-- func_timeout/dafunc.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/func_timeout/__init__.py b/func_timeout/__init__.py index 7ed7e09..febe080 100644 --- a/func_timeout/__init__.py +++ b/func_timeout/__init__.py @@ -9,7 +9,7 @@ __version__ = '3.1.0' __version_tuple__ = (3, 1, 0) -__all__ = ('func_timeout', 'FunctionTimedOut') +__all__ = ('func_timeout', 'set_timeout', 'FunctionTimedOut') from .exceptions import FunctionTimedOut -from .dafunc import func_timeout +from .dafunc import func_timeout, set_timeout diff --git a/func_timeout/dafunc.py b/func_timeout/dafunc.py index a8ce943..87ec7e3 100644 --- a/func_timeout/dafunc.py +++ b/func_timeout/dafunc.py @@ -1,5 +1,5 @@ ''' - Copyright (c) 2016 Tim Savannah All Rights Reserved. + Copyright (c) 2016, 2017 Tim Savannah All Rights Reserved. Licensed under the Lesser GNU Public License Version 3, LGPLv3. You should have recieved a copy of this with the source distribution as LICENSE, otherwise it is available at https://github.com/kata198/func_timeout/LICENSE @@ -10,6 +10,17 @@ import time from .exceptions import FunctionTimedOut from .StoppableThread import StoppableThread +__all__ = ('set_timeout', 'func_timeout') + + +def set_timeout(timeout): + def _function_decorator(func): + def _function_wrapper(*args, **kwargs): + return func_timeout(timeout, 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.