优化线程池

This commit is contained in:
CHAMPION923
2025-06-04 14:15:22 +08:00
parent 7123a1836f
commit c839bc923e

View File

@@ -11,7 +11,7 @@ from multiprocessing import Manager, set_start_method
import json
from lib.io.process3d import Process3D
from lib.io.process2d import Process2D
from collections import deque, OrderedDict
from collections import deque, OrderedDict
import numpy as np
import socket
from lib.camera.ArenaCamera import ArenaCamera
@@ -69,6 +69,8 @@ class Presenter:
self.process2d_info[cfg["title"]] = pro
pro.start()
self.executor = ThreadPoolExecutor(max_workers=8)
print("DEBUG:init completed")
# NOTE: 障碍物状态历史队列
@@ -125,7 +127,6 @@ class Presenter:
# TODO: 避障模式相机采集和数据处理
def obstacle_mode_data_handle(self):
"""获取所有3D避障相机的数据并处理直到所有处理完成再继续"""
st = time.time()
futures = []
# 1. 发出CAPTURE命令
for key in self.process3d_info.keys():
@@ -133,15 +134,15 @@ class Presenter:
continue
self.process3d_info[key].in_q.put(CameraControl.CAPTURE)
st = time.time()
# 2. 等待图像并提交处理任务
while not self.fifo_3d.empty():
img_data = self.fifo_3d.get()
future = self.executor.submit(self.handle_obstacle_data, img_data)
futures.append(future)
et = time.time()
dua = (et - st) * 1000
print(f"[DEBUG]: 采图用时{dua:.2f}ms")
with ThreadPoolExecutor(max_workers=4) as executor:
# 2. 等待图像并提交处理任务
while not self.fifo_3d.empty():
img_data = self.fifo_3d.get()
future = executor.submit(self.handle_obstacle_data, img_data)
futures.append(future)
# 3. 等待所有算法任务完成(阻塞)
for future in as_completed(futures):
@@ -175,7 +176,7 @@ class Presenter:
and self.pkt["b_r_obstacle_distance"] == "000"
else "NG"
)
def wait_rec_tcp_data(self):
pass
@@ -214,7 +215,7 @@ class Presenter:
try:
server.accept_client()
while True:
start_time = time.time()
start_time = time.time()
# TODO: next_time 记录时钟控制帧率
next_time = time.perf_counter() + PERIOD
try:
@@ -265,7 +266,7 @@ class Presenter:
if wait > 0:
time.sleep(wait)
next_time += PERIOD
# TODO: DEBUG
print(f"[DEBUG]:{self.pkt}")
except KeyboardInterrupt: