Commit dd7b0757 by baiquan

refactor(upload): 优化 SKU 图片上传的超时设置

- 将超时时间从 65 秒调整为 85 秒
- 优化超时计算逻辑,确保整个上传过程不超过设定的超时时间
-调整代码结构,提高可读性和维护性
parent 5afcfd97
...@@ -197,6 +197,9 @@ async def uploadImageAndVideo(task: dict = None): ...@@ -197,6 +197,9 @@ async def uploadImageAndVideo(task: dict = None):
error_msg = "" error_msg = ""
# 准备SKU图片上传 # 准备SKU图片上传
sku_image_list = [] sku_image_list = []
# 设置超时时间为85秒
timeout_seconds = 85
start_time = time.time()
try: try:
for sku in skus: for sku in skus:
sku_id = sku.get('sku_id') sku_id = sku.get('sku_id')
...@@ -225,18 +228,17 @@ async def uploadImageAndVideo(task: dict = None): ...@@ -225,18 +228,17 @@ async def uploadImageAndVideo(task: dict = None):
check_image_size(local_path) check_image_size(local_path)
local_path = check_image_width_height(local_path, 2) local_path = check_image_width_height(local_path, 2)
description_list.append({md5_key: local_path}) description_list.append({md5_key: local_path})
# 设置超时时间为60秒
timeout_seconds = 65
start_time = time.time()
# 使用asyncio.wait_for设置超时并行处理所有上传任务 # 使用asyncio.wait_for设置超时并行处理所有上传任务
remaining_time = timeout_seconds - (time.time() - start_time)
sku_image_dict, image_dict, description_dict = await asyncio.wait_for( sku_image_dict, image_dict, description_dict = await asyncio.wait_for(
asyncio.gather( asyncio.gather(
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, sku_image_list), run_in_executor(upload_image_by_bytes, cookies, headers, proxies, sku_image_list),
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, image_list), run_in_executor(upload_image_by_bytes, cookies, headers, proxies, image_list),
run_in_executor(upload_image_by_bytes, cookies, headers, proxies, description_list) run_in_executor(upload_image_by_bytes, cookies, headers, proxies, description_list)
), ),
timeout=timeout_seconds timeout=remaining_time
) )
start_time = time.time()
remaining_time = timeout_seconds - (time.time() - start_time) remaining_time = timeout_seconds - (time.time() - start_time)
image_list = list(image_dict.values()) image_list = list(image_dict.values())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment