将rtsp集成到FastAPI声明周期

This commit is contained in:
zqc
2025-12-21 20:43:33 +08:00
parent 51bf38f84c
commit 4a3105adfc
3 changed files with 73 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ from src.api.errors import (
from src.config import settings
from src.database.connection import init_database
from src.database.connection import db_manager
from src.rtsp.service import rtsp_server
# 生命周期管理
@@ -53,10 +54,25 @@ async def lifespan(app: FastAPI):
else:
print("❌ 数据库连接失败")
# 启动 RTSP 服务(如果启用)
if settings.RTSP_ENABLED:
print("📹 启动 RTSP 服务...")
rtsp_server.start()
# 将 RTSP 服务实例保存到应用状态
app.state.rtsp_server = rtsp_server
else:
print("⚠️ RTSP 服务未启用")
yield
# 关闭时
print("🛑 algorithm service stopped...")
# 停止 RTSP 服务
if settings.RTSP_ENABLED:
print("🛑 停止 RTSP 服务...")
rtsp_server.stop()
db_manager.close()