看了很多博文,试用了很多模块,结果都是只能在Linux系统下才能正常工作,看到该博文:https://www.cnblogs.com/haoxr/p/8757985.html 后才知道原来Windows下也有超时捕获的模块 func_timeout
接下来看看怎么用这个模块
- func_timout
from func_timeout import func_set_timeout, FunctionTimedOut
@func_set_timeout(2.5)
def mytest2():
print("Start")
for i in range(1, 10):
print("%d seconds have passed" % i)
time.sleep(10)
if __name__ == '__main__':
try:
mytest2()
except FunctionTimedOut as e:
print('mytest2:::', e)
输出为:
Start
1 seconds have passed
mytest2::: Function mytest2 (args=()) (kwargs={}) timed out after 2.500000 seconds.