优化错误日志及重试机制

This commit is contained in:
2024-07-23 09:27:59 +08:00
parent ee86bb4e74
commit bcd9f94daf
9 changed files with 64 additions and 52 deletions

View File

@@ -1,13 +1,15 @@
import logging
import math
import urllib.request
from time import sleep
import cv2
import numpy
from paddleclas import PaddleClas
from tenacity import retry, stop_after_attempt, wait_random
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
retry_error_callback=lambda e: logging.warning("获取图片失败", exc_info=e))
def read(image_path):
"""
从网络或本地读取图片
@@ -15,15 +17,8 @@ def read(image_path):
:return: NumPy数组形式的图片
"""
if image_path.startswith("http"):
resp = None
for i in range(3):
# 发送HTTP请求并获取图像数据
try:
resp = urllib.request.urlopen(image_path, timeout=60)
break
except Exception as e:
logging.warning("获取图片失败", exc_info=e)
sleep(3)
# 发送HTTP请求并获取图像数据
resp = urllib.request.urlopen(image_path, timeout=60)
# 将数据读取为字节流
image_data = resp.read()
# 将字节流转换为NumPy数组