def timelimit(timeout):
 def _1(function):
  def _2(*args, **kw):
   class Dispatch(threading.Thread, ):
    def __init__(self):
     threading.Thread.__init__(self)
     self.result = None
     self.error = None
     self.setDaemon(True)
     self.start()
    def run(self):
     try:
      self.result = function(*args, **kw)
     except:
      self.error = sys.exc_info()
   c = Dispatch()
   c.join(timeout)
   if c.isAlive():
    raise TimeoutError()
   if c.error:
    raise c.error[0](c.error[1])
   return c.result
  return _2
 return _1

def timelimit ( timeout ) : def _1 ( function ) : def _2 ( * args , ** kw ) : class Dispatch ( threading . Thread , ) : def __init__ ( self ) : threading . Thread . __init__ ( self ) self . result = None self . error = None self . setDaemon ( True ) self . start ( ) def run ( self ) : try : self . result = function ( * args , ** kw ) except : self . error = sys . exc_info ( ) c = Dispatch ( ) c . join ( timeout ) if c . isAlive ( ) : raise TimeoutError ( ) if c . error : raise c . error [ 0 ] ( c . error [ 1 ] ) return c . result return _2 return _1
a decorator to limit a function to timeout seconds .