优化代码风格
This commit is contained in:
@ -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)
|
||||
# 持续使用时更新最后使用时间
|
||||
|
Reference in New Issue
Block a user