checked 5 sensors run in parralle, cpu max out

This commit is contained in:
bluepulsar
2025-12-27 15:15:42 +08:00
parent a8b02105fc
commit 0b1b7138c0
3 changed files with 233 additions and 41 deletions
+89 -41
View File
@@ -9,21 +9,20 @@ independent CUDA contexts.
import os
def _sensor_worker(sensor_id, config, data_queue, stop_event):
def _sensor_worker(sensor_id, config, data_queue, stop_event, use_async=False):
"""
Worker function that runs in a separate PROCESS for each sensor.
Each process has its own Python interpreter and CUDA context.
Note: This function must be at module level for multiprocessing to work.
Args:
use_async: If True, use 3-stage async pipeline within the process
"""
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
# Import inside process to get fresh CUDA context
from .sensor import Sensor
print(f"[Sensor {sensor_id}] Process started (PID: {os.getpid()})")
print(f"[Sensor {sensor_id}] Process started (PID: {os.getpid()}), async={use_async}")
# Initialize sensor in this process
sensor = Sensor(
vid_src=config.get('vid_src'),
isstitch=config.get('isstitch', True),
@@ -33,41 +32,61 @@ def _sensor_worker(sensor_id, config, data_queue, stop_event):
cal_path=config.get('cal_path', None)
)
# Enable async pipeline if requested
if use_async:
sensor.enable_async(queue_size=2)
frame_count = 0
def queue_put(data):
try:
while not data_queue.empty():
try: data_queue.get_nowait()
except: break
data_queue.put_nowait(data)
except: pass
while not stop_event.is_set():
try:
# Get image and compute deformation
sensor.get_img()
sensor.compute_deformation()
# Package all data
data = {
'sensor_id': sensor_id,
'img': sensor.img.copy() if sensor.img is not None else None,
'flow': sensor.flow.copy() if sensor.flow is not None else None,
'normal': sensor.normal.copy() if sensor.normal is not None else None,
'shear': sensor.shear.copy() if sensor.shear is not None else None,
'depth': sensor.depth.copy() if sensor.depth is not None else None,
'fps': sensor.fps,
'fnormal': sensor.fnormal,
'fshearx': sensor.fshearx,
'fsheary': sensor.fsheary,
'ftor': sensor.ftor,
'frame_count': frame_count,
}
# Non-blocking put - drop old frames if queue is full
try:
while not data_queue.empty():
try:
data_queue.get_nowait()
except:
break
data_queue.put_nowait(data)
except:
pass
if use_async:
# Use async get
result = sensor.get_async(timeout=0.05)
if result is None:
continue
data = {
'sensor_id': sensor_id,
'img': result['img'].copy() if result['img'] is not None else None,
'flow': result['flow'].copy() if result['flow'] is not None else None,
'normal': result['normal'].copy() if result['normal'] is not None else None,
'shear': result['shear'].copy() if result['shear'] is not None else None,
'depth': result['depth'].copy() if result['depth'] is not None else None,
'fps': result['fps'],
'fnormal': result['fnormal'],
'fshearx': result['fshearx'],
'fsheary': result['fsheary'],
'ftor': result['ftor'],
'frame_count': frame_count,
}
else:
# Sync mode
sensor.get_img()
sensor.compute_deformation()
data = {
'sensor_id': sensor_id,
'img': sensor.img.copy() if sensor.img is not None else None,
'flow': sensor.flow.copy() if sensor.flow is not None else None,
'normal': sensor.normal.copy() if sensor.normal is not None else None,
'shear': sensor.shear.copy() if sensor.shear is not None else None,
'depth': sensor.depth.copy() if sensor.depth is not None else None,
'fps': sensor.fps,
'fnormal': sensor.fnormal,
'fshearx': sensor.fshearx,
'fsheary': sensor.fsheary,
'ftor': sensor.ftor,
'frame_count': frame_count,
}
queue_put(data)
frame_count += 1
except Exception as e:
@@ -136,6 +155,7 @@ class MultiSensor:
self.sensor_configs = sensor_configs
self.num_sensors = len(sensor_configs)
self._mp = mp
self._use_async = False
self.processes = []
self.data_queues = []
@@ -147,24 +167,33 @@ class MultiSensor:
self.data_queues.append(mp.Queue(maxsize=2))
self.stop_events.append(mp.Event())
def start(self):
"""Start all sensor worker processes."""
def start(self, use_async=False):
"""
Start all sensor worker processes.
Args:
use_async: If True, each sensor process uses 3-stage async pipeline
for maximum throughput (Capture → Flow → HHD in parallel)
"""
if self._started:
print("MultiSensor already started")
return
self._use_async = use_async
for i, config in enumerate(self.sensor_configs):
process = self._mp.Process(
target=_sensor_worker,
args=(i, config, self.data_queues[i], self.stop_events[i]),
args=(i, config, self.data_queues[i], self.stop_events[i], use_async),
daemon=True
)
process.start()
self.processes.append(process)
print(f"Started sensor {i} (PID: {process.pid})")
mode = "async (3-stage pipeline)" if use_async else "sync"
self._started = True
print(f"All {len(self.processes)} sensors started")
print(f"All {len(self.processes)} sensors started in {mode} mode")
def get(self, sensor_idx, timeout=0.01):
"""
@@ -254,8 +283,8 @@ class MultiSensor:
print("All sensors stopped.")
def __enter__(self):
"""Context manager entry - starts sensors."""
self.start()
"""Context manager entry - starts sensors (sync mode by default)."""
self.start(use_async=False)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -266,4 +295,23 @@ class MultiSensor:
def __del__(self):
"""Cleanup on deletion."""
self.stop()
@classmethod
def async_context(cls, sensor_configs):
"""
Create a MultiSensor that uses async mode when used as context manager.
Example:
with MultiSensor.async_context(configs) as multi:
for all_data in multi:
...
"""
return _AsyncMultiSensorContext(sensor_configs)
class _AsyncMultiSensorContext(MultiSensor):
"""Helper class for async context manager."""
def __enter__(self):
self.start(use_async=True)
return self