From 7ae699082ff9642794b893d616e105a5ee52ce3e Mon Sep 17 00:00:00 2001 From: Mathieu De Coster Date: Wed, 15 May 2024 15:38:56 +0200 Subject: [PATCH] Reuse shared memory file if it still exists --- .../cameras/multiprocess/multiprocess_rgb_camera.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/airo-camera-toolkit/airo_camera_toolkit/cameras/multiprocess/multiprocess_rgb_camera.py b/airo-camera-toolkit/airo_camera_toolkit/cameras/multiprocess/multiprocess_rgb_camera.py index 12ca45d..9625e66 100644 --- a/airo-camera-toolkit/airo_camera_toolkit/cameras/multiprocess/multiprocess_rgb_camera.py +++ b/airo-camera-toolkit/airo_camera_toolkit/cameras/multiprocess/multiprocess_rgb_camera.py @@ -9,6 +9,7 @@ from airo_camera_toolkit.interfaces import RGBCamera from airo_camera_toolkit.utils.image_converter import ImageConverter from airo_typing import CameraIntrinsicsMatrixType, CameraResolutionType, NumpyFloatImageType, NumpyIntImageType +from loguru import logger _RGB_SHM_NAME = "rgb" _RGB_SHAPE_SHM_NAME = "rgb_shape" @@ -28,7 +29,10 @@ def shared_memory_block_like(array: np.ndarray, name: str) -> Tuple[shared_memor Returns: The created shared memory block and a new array that is backed by the shared memory block. """ - shm = shared_memory.SharedMemory(create=True, size=array.nbytes, name=name) + try: + shm = shared_memory.SharedMemory(create=True, size=array.nbytes, name=name) + except FileExistsError: + logger.warning(f"Shared memory file {name} exists. Will try to re-use") shm_array: np.ndarray = np.ndarray(array.shape, dtype=array.dtype, buffer=shm.buf) shm_array[:] = array[:] return shm, shm_array