Skip to content

Commit 022eea8

Browse files
committed
feat: 添加aiofiles支持以实现异步文件操作,优化文件上传性能
1 parent 076e23e commit 022eea8

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

docs/changelog/roadmap.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
## Bugs
99

10-
- [ ] upload 接口会阻塞主进程
1110
- [ ] LightRAG 知识库查看不了解析后的文本,偶然出现,未复现
1211

1312
## Next
@@ -41,4 +40,5 @@
4140
- [x] 基于 create_agent 创建 SQL Viewer 智能体 <Badge type="info" text="0.3.5" />
4241
- [x] 优化 MCP 逻辑,支持 common + special 创建方式 <Badge type="info" text="0.3.5" />
4342
- [x] 修复本地知识库的 metadata 和 向量数据库中不一致的情况。
44-
- [x] v1 版本的 LangGraph 的工具渲染有问题
43+
- [x] v1 版本的 LangGraph 的工具渲染有问题
44+
- [x] upload 接口会阻塞主进程

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies = [
5858
"pypinyin>=0.55.0",
5959
"tomli",
6060
"tomli-w",
61+
"aiofiles>=24.1.0",
6162
]
6263
[tool.ruff]
6364
line-length = 120 # 代码最大行宽

server/routers/knowledge_router.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import aiofiles
12
import asyncio
23
import os
34
import traceback
@@ -593,19 +594,26 @@ async def upload_file(
593594
basename, ext = os.path.splitext(file.filename)
594595
filename = f"{basename}_{hashstr(basename, 4, with_salt=True)}{ext}".lower()
595596
file_path = os.path.join(upload_dir, filename)
596-
os.makedirs(upload_dir, exist_ok=True)
597+
598+
# 在线程池中执行同步文件系统操作,避免阻塞事件循环
599+
await asyncio.to_thread(os.makedirs, upload_dir, exist_ok=True)
597600

598601
file_bytes = await file.read()
599602

600-
content_hash = calculate_content_hash(file_bytes)
601-
if knowledge_base.file_existed_in_db(db_id, content_hash):
603+
# 在线程池中执行计算密集型操作,避免阻塞事件循环
604+
content_hash = await asyncio.to_thread(calculate_content_hash, file_bytes)
605+
606+
# 在线程池中执行同步数据库查询,避免阻塞事件循环
607+
file_exists = await asyncio.to_thread(knowledge_base.file_existed_in_db, db_id, content_hash)
608+
if file_exists:
602609
raise HTTPException(
603610
status_code=409,
604611
detail="数据库中已经存在了相同文件,File with the same content already exists in this database",
605612
)
606613

607-
with open(file_path, "wb") as buffer:
608-
buffer.write(file_bytes)
614+
# 使用异步文件写入,避免阻塞事件循环
615+
async with aiofiles.open(file_path, "wb") as buffer:
616+
await buffer.write(file_bytes)
609617

610618
return {
611619
"message": "File successfully uploaded",

0 commit comments

Comments
 (0)