feat: 添加aiofiles支持以实现异步文件操作,优化文件上传性能
This commit is contained in:
parent
076e23eec3
commit
022eea8fde
@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
## Bugs
|
## Bugs
|
||||||
|
|
||||||
- [ ] upload 接口会阻塞主进程
|
|
||||||
- [ ] LightRAG 知识库查看不了解析后的文本,偶然出现,未复现
|
- [ ] LightRAG 知识库查看不了解析后的文本,偶然出现,未复现
|
||||||
|
|
||||||
## Next
|
## Next
|
||||||
@ -42,3 +41,4 @@
|
|||||||
- [x] 优化 MCP 逻辑,支持 common + special 创建方式 <Badge type="info" text="0.3.5" />
|
- [x] 优化 MCP 逻辑,支持 common + special 创建方式 <Badge type="info" text="0.3.5" />
|
||||||
- [x] 修复本地知识库的 metadata 和 向量数据库中不一致的情况。
|
- [x] 修复本地知识库的 metadata 和 向量数据库中不一致的情况。
|
||||||
- [x] v1 版本的 LangGraph 的工具渲染有问题
|
- [x] v1 版本的 LangGraph 的工具渲染有问题
|
||||||
|
- [x] upload 接口会阻塞主进程
|
||||||
@ -58,6 +58,7 @@ dependencies = [
|
|||||||
"pypinyin>=0.55.0",
|
"pypinyin>=0.55.0",
|
||||||
"tomli",
|
"tomli",
|
||||||
"tomli-w",
|
"tomli-w",
|
||||||
|
"aiofiles>=24.1.0",
|
||||||
]
|
]
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
line-length = 120 # 代码最大行宽
|
line-length = 120 # 代码最大行宽
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import aiofiles
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import traceback
|
import traceback
|
||||||
@ -593,19 +594,26 @@ async def upload_file(
|
|||||||
basename, ext = os.path.splitext(file.filename)
|
basename, ext = os.path.splitext(file.filename)
|
||||||
filename = f"{basename}_{hashstr(basename, 4, with_salt=True)}{ext}".lower()
|
filename = f"{basename}_{hashstr(basename, 4, with_salt=True)}{ext}".lower()
|
||||||
file_path = os.path.join(upload_dir, filename)
|
file_path = os.path.join(upload_dir, filename)
|
||||||
os.makedirs(upload_dir, exist_ok=True)
|
|
||||||
|
# 在线程池中执行同步文件系统操作,避免阻塞事件循环
|
||||||
|
await asyncio.to_thread(os.makedirs, upload_dir, exist_ok=True)
|
||||||
|
|
||||||
file_bytes = await file.read()
|
file_bytes = await file.read()
|
||||||
|
|
||||||
content_hash = calculate_content_hash(file_bytes)
|
# 在线程池中执行计算密集型操作,避免阻塞事件循环
|
||||||
if knowledge_base.file_existed_in_db(db_id, content_hash):
|
content_hash = await asyncio.to_thread(calculate_content_hash, file_bytes)
|
||||||
|
|
||||||
|
# 在线程池中执行同步数据库查询,避免阻塞事件循环
|
||||||
|
file_exists = await asyncio.to_thread(knowledge_base.file_existed_in_db, db_id, content_hash)
|
||||||
|
if file_exists:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=409,
|
status_code=409,
|
||||||
detail="数据库中已经存在了相同文件,File with the same content already exists in this database",
|
detail="数据库中已经存在了相同文件,File with the same content already exists in this database",
|
||||||
)
|
)
|
||||||
|
|
||||||
with open(file_path, "wb") as buffer:
|
# 使用异步文件写入,避免阻塞事件循环
|
||||||
buffer.write(file_bytes)
|
async with aiofiles.open(file_path, "wb") as buffer:
|
||||||
|
await buffer.write(file_bytes)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "File successfully uploaded",
|
"message": "File successfully uploaded",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user