

How does this decorator work? - aaronchall
http://stackoverflow.com/q/31822190/541136

======
aaronchall
The decorator: @timeout(timelimit)

The code:

    
    
      from functools import wraps
      import errno
      import os
      import signal
    
      class TimeoutError(Exception):
        pass
    
      def timeout(seconds=100, 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.alarm(seconds)
                try:
                    result = func(*args, **kwargs)
                finally:
                    signal.alarm(0)
                return result
    
            return wraps(func)(wrapper)
    
        return decorator

