优化代码风格

This commit is contained in:
ZZX9599
2025-09-08 17:34:23 +08:00
parent 9b3d20511a
commit 8ceb92c572
20 changed files with 223 additions and 192 deletions

View File

@ -61,12 +61,12 @@ def _release_engine():
def _monitor_thread():
"""监控线程优化检查逻辑"""
"""监控线程优化检查逻辑"""
global _ref_count, _last_used_time, _ocr_engine
while True:
time.sleep(5) # 每5秒检查一次
with _lock:
# 只有当引擎存在、没有引用且超时才释放
# 只有当引擎存在、没有引用且超时才释放
if _ocr_engine and _ref_count == 0 and not _is_releasing:
elapsed = time.time() - _last_used_time
if elapsed > _release_timeout:
@ -100,7 +100,7 @@ def load_model():
def detect(frame):
"""OCR检测优化引用计数管理"""
"""OCR检测优化引用计数管理"""
global _ocr_engine, _forbidden_words, _conf_threshold, _ref_count, _last_used_time
# 验证前置条件
@ -178,7 +178,7 @@ def detect(frame):
return (False, f"检测错误: {str(e)}")
finally:
# 减少引用计数确保线程安全
# 减少引用计数确保线程安全
with _lock:
_ref_count = max(0, _ref_count - 1)
# 持续使用时更新最后使用时间