添加获取图片的超时和重试,防止一直获取而导致程序卡住

This commit is contained in:
2024-07-20 14:54:59 +08:00
parent 2e7e238c77
commit a71e64c24f
2 changed files with 13 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ def get_private_url(key, bucket=BUCKET):
_, resp = UFILE_HANDLER.head_file(bucket, key) _, resp = UFILE_HANDLER.head_file(bucket, key)
if resp.status_code == -1: if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...") logging.warning(f"uCloud连接失败!即将重试...")
sleep(1) sleep(3)
continue continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}") logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}")
@@ -35,7 +35,7 @@ def copy_file(source_bucket, source_key, target_bucket, target_key):
ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key) ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key)
if resp.status_code == -1: if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...") logging.warning(f"uCloud连接失败!即将重试...")
sleep(1) sleep(3)
continue continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning( logging.warning(
@@ -50,7 +50,7 @@ def upload_file(key, file_path, bucket=BUCKET):
ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None) ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None)
if resp.status_code == -1: if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...") logging.warning(f"uCloud连接失败!即将重试...")
sleep(1) sleep(3)
continue continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}") logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")

View File

@@ -1,6 +1,7 @@
import logging import logging
import math import math
import urllib.request import urllib.request
from time import sleep
import cv2 import cv2
import numpy import numpy
@@ -14,8 +15,15 @@ def read(image_path):
:return: NumPy数组形式的图片 :return: NumPy数组形式的图片
""" """
if image_path.startswith("http"): if image_path.startswith("http"):
# 发送HTTP请求并获取图像数据 resp = None
resp = urllib.request.urlopen(image_path) 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)
# 将数据读取为字节流 # 将数据读取为字节流
image_data = resp.read() image_data = resp.read()
# 将字节流转换为NumPy数组 # 将字节流转换为NumPy数组