Python module which allows you to specify timeouts when calling any existing function, and support for stoppable threads
Go to file
Tim Savannah 32078a8065 Changeup how the function decorator works.
Rename to "set_func_timeout" - Give it 2 possible arguments.

First is either a floating point timeout, or a function which will be
invoked each call of decorated function and passed same arguments, to
calculate the timeout that should be used.

Second is "allowOverride", default False. If True, adds a kwarg to the
function being decorated, "forceTimeout", which if provided will
override any default timeout and prevent a calculation attempt.
2017-05-20 01:52:01 -04:00
doc Bump version displayed in doc 2017-01-04 17:06:28 -05:00
func_timeout Changeup how the function decorator works. 2017-05-20 01:52:01 -04:00
.gitignore 1.0.1 - Remove debug print, add ChangeLog, update documentation a bit 2016-03-15 17:32:32 -04:00
ChangeLog Changelog 2017-05-19 20:50:25 -04:00
LICENSE Initial commit 2016-03-14 22:18:52 -04:00
MANIFEST.in Add simple test program, 'testit' 2017-01-04 17:03:09 -05:00
README.md Note set_timeout decorator in README 2017-05-19 20:49:42 -04:00
README.rst Note set_timeout decorator in README 2017-05-19 20:49:42 -04:00
setup.py Version 4.0.0 2017-05-19 20:49:45 -04:00
testit.py Revert 906f2b9028 on testit.py accidental commit 2017-05-20 00:06:54 -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
	'''

set_timeout

This is a decorator you can use on functions to apply func_timeout. Takes a single argument -- timeout.

Example:

@set_timeout(2.5)
def myFunction(self, arg1, arg2):
	...

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

How it works

func_timeout will run the specified function in a thread with the specified arguments until it returns, raises an exception, or the timeout is exceeded. If there is a return or an exception raised, it will be returned/raised as normal.

If the timeout has exceeded, the "FunctionTimedOut" exception will be raised in the context of the function being called, as well as from the context of "func_timeout". You should have your function catch the "FunctionTimedOut" exception and exit cleanly if possible. Every 2 seconds until your function is terminated, it will continue to raise FunctionTimedOut. The terminating of the timed-out function happens in the context of the thread and will not block main execution.

Support

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

Works on windows, linux/unix, cygwin, mac

ChangeLog can be found at https://raw.githubusercontent.com/kata198/func_timeout/master/ChangeLog

Pydoc can be found at: http://htmlpreview.github.io/?https://github.com/kata198/func_timeout/blob/master/doc/func_timeout.html?vers=1