在Python中利用timeout-decorator库设置函数超时结束,具体方法如下:
1.首先,在Python中安装timeout-decorator库;
pip install timeout-decorator
2.timeout-decorator库安装好后,通过执行以下命令即可设置函数超时结束;
import time
import timeout_decorator
@timeout_decorator.timeout(5)
def mytest():
print("Start")
for i in range(1,10):
time.sleep(1)
print("{} seconds have passed".format(i))
if __name__ == '__main__':
mytest()