class TimeoutError(Exception):
    pass

def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):

    def decorator(func):

        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

def wrapper(*args, **kwargs):
    signal.signal(signal.SIGALRM, _handle_timeout)
signal.setitimer(signal.ITIMER_REAL, seconds)
try:
    result = func(*args, **kwargs)
finally:
    signal.alarm(0)
return result
return wraps(func)(wrapper)
return decorator
How to timeout function in python, timeout less than a second