使用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

最新文章

  1. ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)
  2. Python帮助文档中Iteration iterator iterable 的理解
  3. VR外包团队:长年承接VR虚拟现实外包(应用、游戏、视频、漫游等)
  4. HDU 5925 Coconuts
  5. 16Spring_AOP编程(AspectJ)_最终通知
  6. 杭电1170 Balloon Comes
  7. hdu 携程全球数据中心建设 (球面距离 + 最小生成树)
  8. The TCP/IP parameters for tweaking
  9. javaScript 手写图片轮播
  10. thunk的主要用法
  11. 关于c语言中负数位移位操作的漫谈
  12. MySQL 表名和字段名不要使用保留字命名
  13. 虚拟机上的centos7链接不上网络: activation of network connection failed
  14. codeforces / project Euler 泛做
  15. linux 分区、目录及用途
  16. foreve结束
  17. Python--subprocess系统命令模块-深入
  18. Codeforces 782C. Andryusha and Colored Balloons 搜索
  19. Maximum Average Subarray II LT644
  20. luogu3935 Calculating

热门文章

  1. 负载均衡做集群时关于的session不均衡的解决方案
  2. 野火FreeRTOS计数信号量实验意外处理
  3. 2023 年 CCF 春季测试赛模拟赛 - 1
  4. 去除button默认样式
  5. array copy() 的简单使用
  6. Word12 财务部制作本年年度报告office真题
  7. 0627.selenium请求库*2
  8. 向mysql插入数据报错 pymysql.err.DataError: (1406, "Data too long for column 'class' at row 1") 解决方案
  9. 找出一组数中出现次数最多的数(csp201312-1)
  10. oracle 根据逗号拆分字符串一行转多行