def exit_after(s):
    """
    Use as decorator to exit process if
    function takes longer than s seconds.

    Direct call is available via exit_after(TIMEOUT_IN_S)(fce)(args).

    Inspired by https://stackoverflow.com/a/31667005
    """

    def outer(fn):
        def inner(*args, **kwargs):
            timer = threading.Timer(s, thread.interrupt_main)
            timer.start()
            try:
                result = fn(*args, **kwargs)
            except KeyboardInterrupt:
                raise TimeoutError("Function '{}' hit the timeout ({}s).".format(fn.__name__, s))
            finally:
                timer.cancel()
            return result

        return inner

    return outer
def exit_after ( s ) : def outer ( fn ) : def inner ( * args , ** kwargs ) : timer = threading . Timer ( s , thread . interrupt_main ) timer . start ( ) try : result = fn ( * args , ** kwargs ) except KeyboardInterrupt : raise TimeoutError ( "Function '{}' hit the timeout ({}s)." . format ( fn . __name__ , s ) ) finally : timer . cancel ( ) return result return inner return outer
Use as decorator to exit process if
    function takes longer than s seconds.

    Direct call is available via exit_after(TIMEOUT_IN_S)(fce)(args).

    Inspired by https://stackoverflow.com/a/31667005