Add 'set_timeout' decorator for functions

This commit is contained in:
Tim Savannah 2017-05-19 20:49:41 -04:00
parent 3f4bae15c1
commit f8ec7f492c
2 changed files with 14 additions and 3 deletions

View File

@ -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

View File

@ -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.