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", lifespan=lifespan ) # 注册路由 app.include_router(router) if __name__ == "__main__": import uvicorn uvicorn.run( "app:app", host=settings.host, port=settings.port, reload=settings.debug )