polish code with new pre-commit rule (#2923)

This commit is contained in:
Zero Rains
2025-07-19 23:19:27 +08:00
committed by GitHub
parent b8676d71a8
commit 25698d56d1
424 changed files with 14307 additions and 13518 deletions

View File

@@ -14,9 +14,11 @@
# limitations under the License.
"""
import numpy as np
from multiprocessing.shared_memory import SharedMemory
import numpy as np
def shared_memory_exists(name: str) -> bool:
"""Check if a shared memory block with the given name exists.
@@ -37,8 +39,6 @@ def shared_memory_exists(name: str) -> bool:
return False
class IPCSignal:
"""A shared memory wrapper for inter-process communication using numpy arrays.
@@ -50,12 +50,14 @@ class IPCSignal:
value: Numpy array interface to the shared memory buffer.
"""
def __init__(self,
name: str,
array: np.ndarray,
dtype: np.dtype,
suffix: int = None,
create: bool = True) -> None:
def __init__(
self,
name: str,
array: np.ndarray,
dtype: np.dtype,
suffix: int = None,
create: bool = True,
) -> None:
"""Initialize or connect to a shared memory block.
Args:
@@ -76,18 +78,13 @@ class IPCSignal:
name = name + f".{suffix}"
if create:
assert not shared_memory_exists(
name), f"ShareMemory: {name} already exists"
assert not shared_memory_exists(name), f"ShareMemory: {name} already exists"
self.shm = SharedMemory(create=True, size=array.nbytes, name=name)
self.value: np.ndarray = np.ndarray(array.shape,
dtype=array.dtype,
buffer=self.shm.buf)
self.value: np.ndarray = np.ndarray(array.shape, dtype=array.dtype, buffer=self.shm.buf)
self.value[:] = array # Initialize with input array data
else:
self.shm = SharedMemory(name=name)
self.value: np.ndarray = np.ndarray(array.shape,
dtype=array.dtype,
buffer=self.shm.buf)
self.value: np.ndarray = np.ndarray(array.shape, dtype=array.dtype, buffer=self.shm.buf)
def clear(self) -> None:
"""Release system resources and unlink the shared memory block."""