将rtsp集成到FastAPI声明周期
This commit is contained in:
16
src/app.py
16
src/app.py
@@ -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()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user