Python Forum

Full Version: multiprocessing.shared_memory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can create many instances of multiprocessing shared_memory.
But I can only access the last one that was created by name. Am I doing something wrong?, or is that the limit? Or do I first have to do something else if I want to access the others by name?

I am using Python 3.8.3 rc1 running on window10 version 1903.

def create_memory_names():


    names = []
    for i in range(2):
        a = np.zeros(shape=(FRAMESIZE), dtype=np.uint16)
        shm = multiprocessing.shared_memory.SharedMemory(create=True, size=a.nbytes)
        b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
        b[:] = a[:]
        logging.info(f"creatd name={shm.name}, shape={a.shape}, dtype={a.dtype}")
        names.append(shm.name)

    for i in reversed(names):
        shm = multiprocessing.shared_memory.SharedMemory(name = i)
        b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
        logging.info(f"accessed {i} by name")


create_memory_names()
---------
results in:
__________

INFO:root:creatd name=wnsm_0e727904, shape=(40,), dtype=uint16
INFO:root:creatd name=wnsm_f4f7cd3a, shape=(40,), dtype=uint16
INFO:root:accessed wnsm_f4f7cd3a by name
Traceback (most recent call last):
File "study_sharedmem.py", line 39, in <module>
create_memory_names()
File "study_sharedmem.py", line 34, in create_memory_names
shm = multiprocessing.shared_memory.SharedMemory(name = i)
File "shared_memory.py", line 158, in __init__
h_map = _winapi.OpenFileMapping(
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'wnsm_0e727904'
Please use proper error tags while posting a thread.
sorry. So the error should have been:

Error:
INFO:root:creatd name=wnsm_0e727904, shape=(40,), dtype=uint16 INFO:root:creatd name=wnsm_f4f7cd3a, shape=(40,), dtype=uint16 INFO:root:accessed wnsm_f4f7cd3a by name Traceback (most recent call last): File "study_sharedmem.py", line 39, in <module> create_memory_names() File "study_sharedmem.py", line 34, in create_memory_names shm = multiprocessing.shared_memory.SharedMemory(name = i) File "shared_memory.py", line 158, in __init__ h_map = _winapi.OpenFileMapping( FileNotFoundError: [WinError 2] The system cannot find the file specified: 'wnsm_0e727904'