Function Timeout

Adapted from “Function Timeout” in the Python Decorator Library (https://wiki.python.org/moin/PythonDecoratorLibrary#Function_Timeout).

exception timeout.TimeoutError[source]

Bases: exceptions.Exception

Exception raised when function runs too long.

timeout.timeout(seconds=300, error_message='Function call timed out.')[source]

Creates a decorator which can be used to timeout any long-running function.

Parameters:
  • seconds (float, optional) – Number of seconds a function is permitted to run before a TimeoutError exception is raised. Default is 300 seconds (5 minutes).
  • error_message (str, optional) – Error message to be printed when the TimeoutError exception is raised.
Returns:

Decorator function which can wrap around any other function to terminate if it runs for too long.

Return type:

decorator