带超时的涵数装饰器py版
程序员文章站
2022-05-24 14:29:58
...
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'
上一篇: python高级写法
下一篇: 以应用视角解读如何建设cmdb