使用python设置超时

Python code
 
    
@TraceFunc def WorkInTime(Func, Timeout = 0, * args): ''' If function cann't finished in time then timeout ''' def handler(signum, frame): raise AssertionError try : signal.signal(signal.SIGALRM, handler) signal.alarm(Timeout) ret = Func( * args) signal.alarm(0) return ret except AssertionError: logger.error( " %s conn't finished in %d seconds, timeout! " % (Func. __name__ , Timeout)) return - 1

你可能感兴趣的:(python)