修复:P0+P1 生产稳定性和性能优化(6项)

P0 稳定性修复:
- 告警去重字典添加惰性清理机制,防止长时间运行内存溢出
- Redis 连接断开时显式 close() 后再置 None,防止文件描述符泄漏
- 截图消息 ACK 移至成功路径,失败消息留在 pending list 自动重试

P1 性能优化:
- GPU NMS 添加 torch.no_grad() + 显式释放临时张量,减少显存碎片
- 截图存储改为 Redis 原始 bytes,去掉 Base64 编解码开销(兼容旧格式)
- ROI 配置查询 N+1 改为 get_all_bindings() 单次 JOIN 查询

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 14:05:57 +08:00
parent a9a5457583
commit 5a0265de52
8 changed files with 593 additions and 41 deletions

View File

@@ -884,6 +884,37 @@ class SQLiteManager:
except Exception as e:
logger.error(f"获取摄像头算法绑定失败: {e}")
return []
def get_all_bindings(self) -> List[Dict[str, Any]]:
"""获取所有启用的算法绑定(一次查询,避免 N+1"""
try:
cursor = self._conn.cursor()
cursor.execute("""
SELECT b.bind_id, b.roi_id, b.algo_code, b.params, b.priority,
b.enabled, b.created_at, b.updated_at,
a.algo_name, a.target_class
FROM roi_algo_bind b
LEFT JOIN algorithm_registry a ON b.algo_code = a.algo_code
WHERE b.enabled = 1
ORDER BY b.priority DESC
""")
results = []
for row in cursor.fetchall():
result = dict(zip(
['bind_id', 'roi_id', 'algo_code', 'params', 'priority',
'enabled', 'created_at', 'updated_at', 'algo_name', 'target_class'],
row
))
if result.get('params') and isinstance(result['params'], str):
try:
result['params'] = json.loads(result['params'])
except (json.JSONDecodeError, TypeError):
pass
results.append(result)
return results
except Exception as e:
logger.error(f"获取所有算法绑定失败: {e}")
return []
def delete_roi_algo_bind(self, bind_id: str) -> bool:
"""删除ROI算法绑定"""