Python module which allows you to specify timeouts when calling any existing function, and support for stoppable threads
Go to file
2016-03-15 17:32:32 -04:00
doc Initial Release 2016-03-14 22:37:03 -04:00
func_timeout 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
.gitignore 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
ChangeLog 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
LICENSE Initial commit 2016-03-14 22:18:52 -04:00
MANIFEST.in 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
README.md 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
README.rst 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
setup.py 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00

func_timeout

Python module to support running any existing function with a given timeout.

Package Includes

func_timeout

This is the function wherein you pass the timeout, the function you want to call, and any arguments, and it runs it for up to #timeout# seconds, and will return/raise anything the passed function would otherwise return or raise.

def 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

		@return - The return value that #func# gives
	'''

FunctionTimedOut

Exception raised if the function times out

Example

So, for esxample, if you have a function "doit('arg1', 'arg2')" that you want to limit to running for 5 seconds, with func_timeout you can call it like this:

from func_timeout import func_timeout, FunctionTimedOut

...

try:

	doitReturnValue = func_timeout(5, doit, args=('arg1', 'arg2'))

except FunctionTimedOut:
	print ( "doit('arg1', 'arg2') could not complete within 5 seconds and was terminated.\n")
except Exception as e:
	# Handle any exceptions that doit might raise here

Support

I've tested func_timeout with python 2.7, 3.4, and 3.5. It should work on other versions as well.

ChangeLog can be found at https://github.com/kata198/func_timeout/ChangeLog