Skip to content

Commit

Permalink
Reuse shared memory file if it still exists
Browse files Browse the repository at this point in the history
  • Loading branch information
m-decoster committed May 15, 2024
1 parent c81daac commit 7ae6990
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 7ae6990

Please sign in to comment.