Skip to main content

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

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.

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

Release files for func-timeout 3.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for func-timeout 3.0.1
File Size Uploaded
func_timeout-3.0.1.tar.gz 11.7 kB Details

Release files / func_timeout-3.0.1.tar.gz

Download URL func_timeout-3.0.1.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5cce7b02ccffcd78133b2cc86fd6cf6fc0a00d6124fe059676b4e61052029689
BLAKE2b-256 checksum
How to use checksums
1833383839ebd594ae77bbb72481a014983b69e0c0ea9f6130533298e0d12145
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

4.3.5

1 release file

4.3.4

1 release file

4.3.3

1 release file

4.3.2

1 release file

4.3.1

1 release file

4.3.0

1 release file

4.2.0

1 release file

4.1.0

1 release file

4.0.0

1 release file

3.1.0

1 release file

This release

3.0.1 This release

1 release file

3.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page