简体   繁体   中英

How to access shared memory from SharedMemoryManager in python

What i'm trying to do is to use the new SharedMemoryManager from Python 3.8.
I already successfully shared the memory using only the shared_memory.SharedMemory module
SERVER

import numpy as np
from multiprocessing import shared_memory
img = np.ones((700,700,3), np.int8)
shm = shared_memory.SharedMemory(name='uniquememoryname', create=True, size=img.nbytes)
b = np.ndarray(img.shape, dtype=img.dtype, buffer=shm.buf)
b[:] = img[:]  # Copy the original data into shared memory
print('server started - run client now')
time.sleep(20)
shm.close()
shm.unlink()

CLIENT

import numpy as np
import cv2
from multiprocessing import shared_memory
existing_shm = shared_memory.SharedMemory(name='uniquememoryname')
img = np.ndarray((700,700,3), dtype=np.uint8, buffer=existing_shm.buf)
cv2.imshow('img', img)
cv2.waitKey()
existing_shm.close()

but what i'm trying to do now is to be able to use a manager to create the memory and access them.
What I've got so far:
SERVER

# In the first Python interactive shell
if __name__ == '__main__':
    import time
    import numpy as np
    from multiprocessing.managers import SharedMemoryManager
    from multiprocessing import shared_memory

    img = np.zeros((700,700,3), dtype=np.uint8)

    with SharedMemoryManager(address=('127.0.0.1', 50000), authkey=b'abc') as smm:
        shm = smm.SharedMemory(size=img.nbytes)

        # Now create a NumPy array backed by shared memory
        b = np.ndarray(img.shape, dtype=img.dtype, buffer=shm.buf)
        b[:] = img[:]  # Copy the original data into shared memory

        print('server started - run client now')

        time.sleep(20)


    print('server end')

CLIENT

from multiprocessing.managers import SharedMemoryManager
# Attach to the existing shared memory block.
if __name__ == '__main__':
    smm = SharedMemoryManager(address=('127.0.0.1', 50000), authkey=b'abc')
    smm.connect()
    #what do i do now to acess the img from server

So what do I need to do now in CLIENT to access b from server?

As far as I know there is no way to get the list of currently managed memory. You need a way to communicate between server and client and pass the name along. Once you get the SharedMemory object from the manager, you can get the name that was assigned with shm.name. A multiprocessing Queue is probably easiest, the name is just a string so it's easy to send with any number of communication libraries if you need something more complicated.

SERVER

# In the first Python interactive shell
if __name__ == '__main__':
    import time
    import numpy as np
    from multiprocessing.managers import SharedMemoryManager
    from multiprocessing import shared_memory

    img = np.zeros((700,700,3), dtype=np.uint8)

    with SharedMemoryManager(address=('127.0.0.1', 50000), authkey=b'abc') as smm:
        shm = smm.SharedMemory(size=img.nbytes)
        name = shm.name
        # Here you need a way to communicate with the client to send the name

        # Now create a NumPy array backed by shared memory
        b = np.ndarray(img.shape, dtype=img.dtype, buffer=shm.buf)
        b[:] = img[:]  # Copy the original data into shared memory

        print('server started - run client now')

        time.sleep(20)


    print('server end')

CLIENT

from multiprocessing.managers import SharedMemoryManager
from multiprocessing.shared_memory import SharedMemory
# Attach to the existing shared memory block.
if __name__ == '__main__':
    smm = SharedMemoryManager(address=('127.0.0.1', 50000), authkey=b'abc')
    smm.connect()
    # Here you need a way to access the unique name
    shm = SharedMemory(name=name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM