python func_timeout 设置函数超时退出
2024-10-21 11:54:47
使用func_timeout设置函数超时退出,使用func_set_timeout装饰器和func_timeout方法
from func_timeout import func_set_timeout,exceptions.FunctionTimedOut
import time @func_set_timeout(3)
def task():
print('hello world')
time.sleep(5)
return '执行成功_未超时' if __name__ == '__main__':
try:
print(task())
except exceptions.FunctionTimedOut:
print('执行函数超时')
应用场景:用opencv链接网络摄像头,但是摄像头的地址不一定是正确的,或者是打开的,当错误的时候会直接报错,但是当地址正确却没有打开,或者网络限制的时候,会等上半天才返回,并且不报错,此时使用func_timeout进行处理
from func_timeout import func_timeout, FunctionTimedOut
def catch_video(path):
start_time = time.time()
try:
cap = func_timeout(5, cv.VideoCapture, (path,))
if cap.isOpened():
open_time = time.time() - start_time
message = 'video open time is :' + "{:.2f}".format(open_time)
else:
message = 'can not open the video.'
cap.release()
except FunctionTimedOut:
message = 'open video is timeout'
except:
message = 'other error'
# print(message)
return message
最新文章
- ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)
- Python帮助文档中Iteration iterator iterable 的理解
- VR外包团队:长年承接VR虚拟现实外包(应用、游戏、视频、漫游等)
- HDU 5925 Coconuts
- 16Spring_AOP编程(AspectJ)_最终通知
- 杭电1170 Balloon Comes
- hdu 携程全球数据中心建设 (球面距离 + 最小生成树)
- The TCP/IP parameters for tweaking
- javaScript 手写图片轮播
- thunk的主要用法
- 关于c语言中负数位移位操作的漫谈
- MySQL 表名和字段名不要使用保留字命名
- 虚拟机上的centos7链接不上网络: activation of network connection failed
- codeforces / project Euler 泛做
- linux 分区、目录及用途
- foreve结束
- Python--subprocess系统命令模块-深入
- Codeforces 782C. Andryusha and Colored Balloons 搜索
- Maximum Average Subarray II LT644
- luogu3935 Calculating
热门文章
- 负载均衡做集群时关于的session不均衡的解决方案
- 野火FreeRTOS计数信号量实验意外处理
- 2023 年 CCF 春季测试赛模拟赛 - 1
- 去除button默认样式
- array copy() 的简单使用
- Word12 财务部制作本年年度报告office真题
- 0627.selenium请求库*2
- 向mysql插入数据报错 pymysql.err.DataError: (1406, ";Data too long for column 'class' at row 1";) 解决方案
- 找出一组数中出现次数最多的数(csp201312-1)
- oracle 根据逗号拆分字符串一行转多行