mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
使用aiofiles实现异步文件操作,提升性能;调整uvicorn工作进程数量。
This commit is contained in:
@@ -11,7 +11,6 @@ from typing import Optional, Union, Annotated
|
|||||||
import aiofiles
|
import aiofiles
|
||||||
import pillow_avif # noqa 用于自动注册AVIF支持
|
import pillow_avif # noqa 用于自动注册AVIF支持
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from app.helper.sites import SitesHelper
|
|
||||||
from fastapi import APIRouter, Body, Depends, HTTPException, Header, Request, Response
|
from fastapi import APIRouter, Body, Depends, HTTPException, Header, Request, Response
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
@@ -30,6 +29,7 @@ from app.helper.mediaserver import MediaServerHelper
|
|||||||
from app.helper.message import MessageHelper
|
from app.helper.message import MessageHelper
|
||||||
from app.helper.progress import ProgressHelper
|
from app.helper.progress import ProgressHelper
|
||||||
from app.helper.rule import RuleHelper
|
from app.helper.rule import RuleHelper
|
||||||
|
from app.helper.sites import SitesHelper
|
||||||
from app.helper.subscribe import SubscribeHelper
|
from app.helper.subscribe import SubscribeHelper
|
||||||
from app.helper.system import SystemHelper
|
from app.helper.system import SystemHelper
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
@@ -396,8 +396,9 @@ async def get_logging(request: Request, length: Optional[int] = 50, logfile: Opt
|
|||||||
# 返回全部日志作为文本响应
|
# 返回全部日志作为文本响应
|
||||||
if not log_path.exists():
|
if not log_path.exists():
|
||||||
return Response(content="日志文件不存在!", media_type="text/plain")
|
return Response(content="日志文件不存在!", media_type="text/plain")
|
||||||
with open(log_path, "r", encoding='utf-8') as file:
|
# 使用 aiofiles 异步读取文件
|
||||||
text = file.read()
|
async with aiofiles.open(log_path, mode="r", encoding="utf-8") as file:
|
||||||
|
text = await file.read()
|
||||||
# 倒序输出
|
# 倒序输出
|
||||||
text = "\n".join(text.split("\n")[::-1])
|
text = "\n".join(text.split("\n")[::-1])
|
||||||
return Response(content=text, media_type="text/plain")
|
return Response(content=text, media_type="text/plain")
|
||||||
|
|||||||
+10
-9
@@ -2,6 +2,7 @@ import gzip
|
|||||||
import json
|
import json
|
||||||
from typing import Annotated, Callable, Any, Dict, Optional
|
from typing import Annotated, Callable, Any, Dict, Optional
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Path, Request, Response
|
from fastapi import APIRouter, Depends, HTTPException, Path, Request, Response
|
||||||
from fastapi.responses import PlainTextResponse
|
from fastapi.responses import PlainTextResponse
|
||||||
from fastapi.routing import APIRoute
|
from fastapi.routing import APIRoute
|
||||||
@@ -66,17 +67,17 @@ async def update_cookie(req: schemas.CookieData):
|
|||||||
"""
|
"""
|
||||||
file_path = settings.COOKIE_PATH / f"{req.uuid}.json"
|
file_path = settings.COOKIE_PATH / f"{req.uuid}.json"
|
||||||
content = json.dumps({"encrypted": req.encrypted})
|
content = json.dumps({"encrypted": req.encrypted})
|
||||||
with open(file_path, encoding="utf-8", mode="w") as file:
|
async with aiofiles.open(file_path, encoding="utf-8", mode="w") as file:
|
||||||
file.write(content)
|
await file.write(content)
|
||||||
with open(file_path, encoding="utf-8", mode="r") as file:
|
async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
|
||||||
read_content = file.read()
|
read_content = await file.read()
|
||||||
if read_content == content:
|
if read_content == content:
|
||||||
return {"action": "done"}
|
return {"action": "done"}
|
||||||
else:
|
else:
|
||||||
return {"action": "error"}
|
return {"action": "error"}
|
||||||
|
|
||||||
|
|
||||||
def load_encrypt_data(uuid: str) -> Dict[str, Any]:
|
async def load_encrypt_data(uuid: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
加载本地加密原始数据
|
加载本地加密原始数据
|
||||||
"""
|
"""
|
||||||
@@ -87,8 +88,8 @@ def load_encrypt_data(uuid: str) -> Dict[str, Any]:
|
|||||||
raise HTTPException(status_code=404, detail="Item not found")
|
raise HTTPException(status_code=404, detail="Item not found")
|
||||||
|
|
||||||
# 读取文件
|
# 读取文件
|
||||||
with open(file_path, encoding="utf-8", mode="r") as file:
|
async with aiofiles.open(file_path, encoding="utf-8", mode="r") as file:
|
||||||
read_content = file.read()
|
read_content = await file.read()
|
||||||
data = json.loads(read_content.encode("utf-8"))
|
data = json.loads(read_content.encode("utf-8"))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@@ -120,7 +121,7 @@ async def get_cookie(
|
|||||||
"""
|
"""
|
||||||
GET 下载加密数据
|
GET 下载加密数据
|
||||||
"""
|
"""
|
||||||
return load_encrypt_data(uuid)
|
return await load_encrypt_data(uuid)
|
||||||
|
|
||||||
|
|
||||||
@cookie_router.post("/get/{uuid}")
|
@cookie_router.post("/get/{uuid}")
|
||||||
@@ -130,5 +131,5 @@ async def post_cookie(
|
|||||||
"""
|
"""
|
||||||
POST 下载加密数据
|
POST 下载加密数据
|
||||||
"""
|
"""
|
||||||
data = load_encrypt_data(uuid)
|
data = await load_encrypt_data(uuid)
|
||||||
return get_decrypted_cookie_data(uuid, request.password, data["encrypted"])
|
return get_decrypted_cookie_data(uuid, request.password, data["encrypted"])
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
+1
-1
@@ -25,7 +25,7 @@ setproctitle.setproctitle(settings.PROJECT_NAME)
|
|||||||
|
|
||||||
# uvicorn服务
|
# uvicorn服务
|
||||||
Server = uvicorn.Server(Config(app, host=settings.HOST, port=settings.PORT,
|
Server = uvicorn.Server(Config(app, host=settings.HOST, port=settings.PORT,
|
||||||
reload=settings.DEV, workers=multiprocessing.cpu_count(),
|
reload=settings.DEV, workers=multiprocessing.cpu_count() * 2 + 1,
|
||||||
timeout_graceful_shutdown=60))
|
timeout_graceful_shutdown=60))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user