上一段简写代码
from threading import Thread
def timeout(seconds = 5,hint=''):
def handler():
raise Exception(f'{hint}执行超时!timeout = {seconds}s')
def wrap(func):
def outter(*args,**kwargs):
signal.signal(signal.SIGALRM,handler)
signal.alarm(seconds)
res = func(*args,**kwargs)
signal.alarm(0)
return res
return outter
return wrap
def fun(a):
print(a)
@timeout(10,'请求资源')
def threadfunc():
t = Thread(fun,args=(1,))
t.start()
t.join()
相关文章地址:
https://www.cnblogs.com/mymelody/p/9299155.html
https://blog.csdn.net/jhonguy/article/details/7716257
网友评论