From e0bf964ff0f8e44d180bb9ab345b41258ee45af9 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 18 Oct 2022 16:28:57 -0400 Subject: [PATCH] Mod define `_USE_POSIX`, add a of of todos --- tractor/_shm.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/tractor/_shm.py b/tractor/_shm.py index c26c991..79ac896 100644 --- a/tractor/_shm.py +++ b/tractor/_shm.py @@ -30,15 +30,19 @@ from multiprocessing import shared_memory as shm from multiprocessing.shared_memory import ( SharedMemory, ShareableList, - # _USE_POSIX, # type: ignore ) -if getattr(shm, '_USE_POSIX', False): - from _posixshmem import shm_unlink - from msgspec import Struct import tractor +from .log import get_logger + + +_USE_POSIX = getattr(shm, '_USE_POSIX', False) +if _USE_POSIX: + from _posixshmem import shm_unlink + + try: import numpy as np from numpy.lib import recfunctions as rfn @@ -46,8 +50,6 @@ try: except ImportError: pass -from .log import get_logger - log = get_logger(__name__) @@ -161,6 +163,8 @@ class NDToken(Struct, frozen=True): # _known_tokens = tractor.ContextStack('_known_tokens', ) # _known_tokens = trio.RunVar('shms', {}) +# TODO: this should maybe be provided via +# a `.trionics.maybe_open_context()` wrapper factory? # process-local store of keys to tokens _known_tokens: dict[str, NDToken] = {} @@ -712,8 +716,12 @@ def maybe_open_shm_ndarray( class ShmList(ShareableList): ''' - Carbon copy of ``.shared_memory.ShareableList`` but add a - readonly state instance var. + Carbon copy of ``.shared_memory.ShareableList`` with a few + enhancements: + + - readonly mode via instance var flag + - ``.__getitem__()`` accepts ``slice`` inputs + - exposes the underlying buffer "name" as a ``.key: str`` ''' def __init__( @@ -752,11 +760,22 @@ class ShmList(ShareableList): self, indexish, ) -> list: + + # NOTE: this is a non-writeable view (copy?) of the buffer + # in a new list instance. if isinstance(indexish, slice): return list(self)[indexish] return super().__getitem__(indexish) + # TODO: should we offer a `.array` and `.push()` equivalent + # to the `ShmArray`? + # currently we have the following limitations: + # - can't write slices of input using traditional slice-assign + # syntax due to the ``ShareableList.__setitem__()`` implementation. + # - ``list(shmlist)`` returns a non-mutable copy instead of + # a writeable view which would be handier numpy-style ops. + def open_shm_list( key: str,