Linux 拨号vps windows公众号手机端

Python怎么设置函数超时结束

lewis 6年前 (2019-03-14) 阅读数 11 #程序编程
文章标签 python

在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()

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门