优化代码风格

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

@ -22,7 +22,7 @@ _task_counter_lock = threading.Lock() # 任务计数锁
# -------------------------- 工具函数 --------------------------
def _get_next_task_id():
"""获取唯一任务ID用于日志追踪"""
"""获取唯一任务ID用于日志追踪"""
global _task_counter
with _task_counter_lock:
_task_counter += 1
@ -65,11 +65,11 @@ def _init_thread_pool():
max_workers=MAX_WORKERS,
thread_name_prefix="DetectionThread"
)
print(f"=== 线程池初始化完成最大线程数: {MAX_WORKERS} ===")
print(f"=== 线程池初始化完成最大线程数: {MAX_WORKERS} ===")
def shutdown():
"""关闭线程池释放资源"""
"""关闭线程池释放资源"""
global _executor
with _executor_lock:
if _executor is not None:
@ -82,7 +82,7 @@ def shutdown():
def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple:
"""在子线程中执行检测逻辑"""
thread_name = threading.current_thread().name
print(f"任务[{task_id}] 开始执行线程: {thread_name}")
print(f"任务[{task_id}] 开始执行线程: {thread_name}")
try:
# 按照优先级执行检测
@ -98,7 +98,7 @@ def _detect_in_thread(frame: np.ndarray, task_id: int) -> tuple:
print(f"任务[{task_id}] {detector}检测结果: {'成功' if flag else '失败'}")
if flag:
print(f"任务[{task_id}] 完成检测使用检测器: {detector}")
print(f"任务[{task_id}] 完成检测使用检测器: {detector}")
return (True, result, detector, task_id)
# 所有检测器均未检测到结果
@ -116,14 +116,14 @@ def detect(frame: np.ndarray) -> Future:
提交检测任务到线程池
参数:
frame: 待检测图像(ndarray格式cv2.imdecode生成)
frame: 待检测图像(ndarray格式cv2.imdecode生成)
返回:
Future对象通过result()方法获取检测结果
Future对象通过result()方法获取检测结果
"""
# 确保模型已加载
if not _model_loaded:
print("警告: 模型尚未加载将自动加载")
print("警告: 模型尚未加载将自动加载")
load_model()
# 生成任务ID
@ -131,6 +131,6 @@ def detect(frame: np.ndarray) -> Future:
# 提交任务到线程池
future = _executor.submit(_detect_in_thread, frame, task_id)
print(f"任务[{task_id}] 已提交到线程池")
print(f"任务[{task_id}]: 已提交到线程池")
return future