优化IO问题,使用异步方案进行视频下载等操作。

This commit is contained in:
liuwei
2025-06-16 10:11:43 +08:00
parent 34f9158697
commit 02a387628c
5 changed files with 68 additions and 58 deletions
+7 -6
View File
@@ -1,6 +1,7 @@
import base64
import io
import os
import aiofiles
import aiohttp
import pysilk
@@ -234,7 +235,7 @@ class ToolMixin(WechatAPIClientBase):
return False
@staticmethod
def base64_to_file(base64_str: str, file_name: str, file_path: str) -> bool:
async def base64_to_file(base64_str: str, file_name: str, file_path: str) -> bool:
"""将base64字符串转换为文件并保存。
Args:
@@ -256,8 +257,8 @@ class ToolMixin(WechatAPIClientBase):
base64_str = base64_str.split(',')[1]
# 解码 base64 并写入文件
with open(full_path, 'wb') as f:
f.write(base64.b64decode(base64_str))
async with aiofiles.open(full_path, 'wb') as f:
await f.write(base64.b64decode(base64_str))
return True
@@ -265,7 +266,7 @@ class ToolMixin(WechatAPIClientBase):
return False
@staticmethod
def file_to_base64(file_path: str) -> str:
async def file_to_base64(file_path: str) -> str:
"""将文件转换为base64字符串。
Args:
@@ -274,8 +275,8 @@ class ToolMixin(WechatAPIClientBase):
Returns:
str: base64编码的字符串
"""
with open(file_path, 'rb') as f:
return base64.b64encode(f.read()).decode()
async with aiofiles.open(file_path, 'rb') as f:
return base64.b64encode(await f.read()).decode()
@staticmethod
def base64_to_byte(base64_str: str) -> bytes: