def timelimit(timeout):
def _1(function):<EOL><INDENT>def _2(*args, **kw):<EOL><INDENT>class Dispatch(threading.Thread):<EOL><INDENT>def __init__(self):<EOL><INDENT>threading.Thread.__init__(self)<EOL>self.result = None<EOL>self.error = None<EOL>self.setDaemon(True)<EOL>self.start()<EOL><DEDENT>def run(self):<EOL><INDENT>try:<EOL><INDENT>self.result = function(*args, **kw)<EOL><DEDENT>except:<EOL><INDENT>self.error = sys.exc_info()<EOL><DEDENT><DEDENT><DEDENT>c = Dispatch()<EOL>c.join(timeout)<EOL>if c.isAlive():<EOL><INDENT>raise TimeoutError('<STR_LIT>')<EOL><DEDENT>if c.error:<EOL><INDENT>raise c.error[<NUM_LIT:0>](c.error[<NUM_LIT:1>])<EOL><DEDENT>return c.result<EOL><DEDENT>return _2<EOL><DEDENT>return _1<EOL>
borrowed from web.py