python函数超时自动退出的实操方法
作者:十一 时间:2021-09-11 07:07:45
本章给大家在项目使用时候,常见的一种情况解决案例,即是当我们调用多个线程,使用了同一个函数去处理数据的时候,有些用函数已经处理完成,但是有些还没有,这就需要我们将任务进行分割,然后当一小部分任务执行后,退出来,另外没有执行的完成超时的就继续去执行,下面就针对遇到这些问题的小伙伴,给大家提供解决参考。
安装timeout-decorator库:
pip3 install timeout-decorator
编写异常语句:
@timeout_decorator.timeout(5, timeout_exception=StopIteration)
函数限制超时:
@timeout_decorator.timeout(5, use_signals=False)
解决案例:
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))
def main():
mytest()
if __name__ == '__main__':
main()
来源:https://www.py.cn/jishu/jichu/22286.html
标签:python,函数超时,自动退出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用共享连接减少空闲的连接数?
2010-05-16 15:15:00
python 安装virtualenv和virtualenvwrapper的方法
2023-04-23 11:27:33
交互设计实用指南系列(6) –标签明晰、有效
2010-01-21 12:39:00
![](https://img.aspxhome.com/file/UploadPic/20101/21/32-66s.jpg)
Python3.10.4激活venv环境失败解决方法
2022-11-15 10:39:39
![](https://img.aspxhome.com/file/2023/4/75984_0s.png)
解读MySQL的InnoDB引擎日志工作原理
2011-01-04 19:59:00
Python 列表的基本操作介绍
2021-09-09 08:13:34
如何使用微信公众平台开发模式实现多客服
2023-11-14 17:48:37
TensorFlow实现简单线性回归
2023-09-18 13:23:45
![](https://img.aspxhome.com/file/2023/9/92519_0s.jpg)
python使用js2py库运行js代码
2022-09-06 17:06:55
Python基于正则表达式实现检查文件内容的方法【文件检索】
2021-06-11 07:58:45
IE下img多余5像素空白
2009-06-08 13:17:00
Python中集合的内建函数和内建方法学习教程
2023-11-03 04:11:27
![](https://img.aspxhome.com/file/2023/2/62892_0s.png)
pandas按照列的值排序(某一列或者多列)
2022-08-14 08:54:08
Python双链表原理与实现方法详解
2023-10-10 20:55:40
PHPMailer发送邮件功能实现流程
2023-06-03 13:11:18
![](https://img.aspxhome.com/file/2023/7/55327_0s.png)
对YOLOv3模型调用时候的python接口详解
2023-05-31 00:58:57
![](https://img.aspxhome.com/file/2023/3/93513_0s.jpg)
PyQt5每天必学之拖放事件
2021-02-28 19:26:15
![](https://img.aspxhome.com/file/2023/1/97191_0s.png)
巧妙的自关联运用
2012-10-07 10:55:58
自己重新写了一个JavaScript的对象克隆函数
2008-08-03 16:47:00
python中import与from方法总结(推荐)
2021-12-19 12:31:06