添加获取图片的超时和重试,防止一直获取而导致程序卡住
This commit is contained in:
@@ -15,7 +15,7 @@ def get_private_url(key, bucket=BUCKET):
|
||||
_, resp = UFILE_HANDLER.head_file(bucket, key)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(1)
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
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)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(1)
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
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)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(1)
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
import math
|
||||
import urllib.request
|
||||
from time import sleep
|
||||
|
||||
import cv2
|
||||
import numpy
|
||||
@@ -14,8 +15,15 @@ def read(image_path):
|
||||
:return: NumPy数组形式的图片
|
||||
"""
|
||||
if image_path.startswith("http"):
|
||||
# 发送HTTP请求并获取图像数据
|
||||
resp = urllib.request.urlopen(image_path)
|
||||
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)
|
||||
# 将数据读取为字节流
|
||||
image_data = resp.read()
|
||||
# 将字节流转换为NumPy数组
|
||||
|
||||
Reference in New Issue
Block a user