引擎计算超时
This commit is contained in:
		
							
								
								
									
										13
									
								
								core/face.py
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								core/face.py
									
									
									
									
									
								
							| @ -27,7 +27,8 @@ _used_gpu_id = -1  # 使用的GPU ID(-1表示CPU) | ||||
|  | ||||
| # 资源管理变量 | ||||
| _ref_count = 0  # 引擎引用计数(记录当前使用次数) | ||||
| _last_used_time = 0  # 最后一次使用引擎的时间 | ||||
| # 修复点1:初始值设为当前时间,避免未加载引擎时用0计算超时 | ||||
| _last_used_time = time.time() | ||||
| _lock = threading.Lock()  # 线程安全锁 | ||||
| _release_timeout = 8  # 闲置超时时间(秒) | ||||
| _is_releasing = False  # 资源释放中标记 | ||||
| @ -77,7 +78,7 @@ def select_best_gpu(preferred_gpus=[0, 1]): | ||||
|  | ||||
| def _release_engine_resources(): | ||||
|     """释放人脸引擎的所有资源(模型、特征库、GPU缓存等)""" | ||||
|     global _face_app, _is_releasing, _known_faces_embeddings, _known_faces_names | ||||
|     global _face_app, _is_releasing, _known_faces_embeddings, _known_faces_names, _last_used_time | ||||
|     if not _face_app or _is_releasing: | ||||
|         return | ||||
|  | ||||
| @ -131,6 +132,8 @@ def _release_engine_resources(): | ||||
|         print(f"释放资源过程中出错: {e}") | ||||
|     finally: | ||||
|         _is_releasing = False | ||||
|         # 修复点2:释放完成后重置最后使用时间,避免下次加载时复用旧值 | ||||
|         _last_used_time = time.time() | ||||
|  | ||||
|  | ||||
| def _resource_monitor_thread(): | ||||
| @ -150,7 +153,7 @@ def _resource_monitor_thread(): | ||||
|  | ||||
| def load_model(prefer_gpu=True, preferred_gpus=[0, 1]): | ||||
|     """加载人脸识别引擎及已知人脸特征库(默认优先用0号GPU)""" | ||||
|     global _face_app, _known_faces_embeddings, _known_faces_names, _using_gpu, _used_gpu_id | ||||
|     global _face_app, _known_faces_embeddings, _known_faces_names, _using_gpu, _used_gpu_id, _last_used_time | ||||
|  | ||||
|     # 启动后台监控线程(确保仅启动一次) | ||||
|     if not _monitor_thread_running: | ||||
| @ -189,6 +192,10 @@ def load_model(prefer_gpu=True, preferred_gpus=[0, 1]): | ||||
|         # 准备模型(加载到指定设备) | ||||
|         _face_app.prepare(ctx_id=ctx_id, det_size=(640, 640)) | ||||
|         print("InsightFace引擎初始化完成") | ||||
|  | ||||
|         # 修复点3:引擎初始化成功后,立即更新“最后使用时间”(核心修复) | ||||
|         _last_used_time = time.time() | ||||
|  | ||||
|         _debug_counter["engine_created"] += 1 | ||||
|         print(f"引擎调试统计: {_debug_counter}") | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user