带超时的涵数装饰器py版

阅读更多
import time
import signal


def fun_timeout(time_out):
    def wrapper(fun):
        def func(*args, **kwargs):
            def handler(signum, frame):
                raise AssertionError
try:
                signal.signal(signal.SIGALRM, handler)
                signal.alarm(time_out)
                return fun(*args, **kwargs)
            except AssertionError:
                print('timeout')
                return 'timeout'
return func
    return wrapper


@fun_timeout(5)
def t1():
    time.sleep(3)
    print('t1')
    return 't1'

你可能感兴趣的:(超时,装饰器)