简体   繁体   English

如何从python中的SharedMemoryManager访问共享内存

[英]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.我想要做的是使用 Python 3.8 中的新 SharedMemoryManager。
I already successfully shared the memory using only the shared_memory.SharedMemory module我已经成功地仅使用shared_memory.SharedMemory模块共享了内存
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?那么我现在需要在 CLIENT 中做什么才能从服务器访问b

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.从管理器中获取 SharedMemory 对象后,您就可以获取使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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