def timeout(duration):
    """
    A decorator to force a time limit on the execution of an external function.

    :param int duration: the timeout duration

    :raises: TypeError, if duration is anything other than integer

    :raises: ValueError, if duration is a negative integer

    :raises TimeoutError, if the external function execution crosses 'duration' time

    """
    if not isinstance(duration, int):
        raise TypeError("timeout duration should be a positive integer")
    if duration <= 0:
        raise ValueError("timeoutDuration should be a positive integer")

    def decorator(func):
        def wrapped_func(*args, **kwargs):
            try:
                def alarm_handler(signum, stack):
                    raise TimeoutError()
                signal.signal(signal.SIGALRM, alarm_handler)
                signal.alarm(duration)
                reply = func(*args, **kwargs)
            except TimeoutError as e:
                raise e
            return reply
        return wrapped_func
    return decorator
def timeout ( duration ) : if not isinstance ( duration , int ) : raise TypeError ( "timeout duration should be a positive integer" ) if duration <= 0 : raise ValueError ( "timeoutDuration should be a positive integer" ) def decorator ( func ) : def wrapped_func ( * args , ** kwargs ) : try : def alarm_handler ( signum , stack ) : raise TimeoutError ( ) signal . signal ( signal . SIGALRM , alarm_handler ) signal . alarm ( duration ) reply = func ( * args , ** kwargs ) except TimeoutError as e : raise e return reply return wrapped_func return decorator
A decorator to force a time limit on the execution of an external function.

    :param int duration: the timeout duration

    :raises: TypeError, if duration is anything other than integer

    :raises: ValueError, if duration is a negative integer

    :raises TimeoutError, if the external function execution crosses 'duration' time