From 7f03cc24e3504216ae512843de4b4e9583bf98a2 Mon Sep 17 00:00:00 2001 From: jingrow Date: Sun, 23 Nov 2025 16:33:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(rmbg):=20=E5=B0=86=20on=5Fevent=20=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E5=88=B0=20lifespan=20=E4=BA=8B=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=E4=BB=A5=E6=B6=88=E9=99=A4=E5=BC=83=E7=94=A8?= =?UTF-8?q?=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/rmbg/app.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/rmbg/app.py b/apps/rmbg/app.py index 2a7743e..8b6e9c7 100644 --- a/apps/rmbg/app.py +++ b/apps/rmbg/app.py @@ -1,29 +1,30 @@ +from contextlib import asynccontextmanager from fastapi import FastAPI from settings import settings from api import router, service + +@asynccontextmanager +async def lifespan(app: FastAPI): + """应用生命周期管理""" + # 启动时初始化 + if settings.enable_queue_batch: + await service._start_queue_processor() + yield + # 关闭时清理资源 + await service.cleanup() + + app = FastAPI( title="Remove Background", description="图片去背景", - version="1.0.0" + version="1.0.0", + lifespan=lifespan ) # 注册路由 app.include_router(router) - -@app.on_event("startup") -async def startup_event(): - """应用启动时初始化队列批处理机制""" - if settings.enable_queue_batch: - await service._start_queue_processor() - - -@app.on_event("shutdown") -async def shutdown_event(): - """应用关闭时清理资源""" - await service.cleanup() - if __name__ == "__main__": import uvicorn uvicorn.run(