Shared memory between Python processes (version 3.8+)

To use multiprocessing.shared_memory, the Python version requires 3.8+, which is applicable to both Windows and Linnux. The prototype is as follows,

# name: 共享内存名字,为None时会自动生成一个名字
# create: 是否创建共享内存,True则创建,False则使用已经存在的共享内存
# size: 希望创建的共享内存大小,create为True时有效(单位:byte)
class multiprocessing.shared_memory.SharedMemory(name=None, create=False, size=0)

Processes interact or communicate through the name of shared memory.


example

Process A creates shared memory with the name shmTest555666

from multiprocessing import shared_memory


shm = shared_memory.SharedMemory(name='shmTest555666', create=True, size=10)

buf = shm.buf
print('Process A, buf len: {}, {}'.format(len(buf), shm.size))
print('shm name: {}'.format(shm.name))

buf[:4] = bytearray([22, 33, 44, 55])
buf[4] = 100


while True:
    if buf[4] == 200: # 当buf[4]变成200,就结束循环
        break


shm.close() # 关闭共享内存
shm.unlink() # 释放共享内存,也可以由B进程释放

Process B uses the shared memory created by A, and can find and modify the shared memory of A through the name shmTest555666.

from multiprocessing import shared_memory


shm = shared_memory.SharedMemory(name='shmTest555666')
buf = shm.buf
print('Process B, buf len: {}, {}'.format(len(buf), shm.size))
print('shm name: {}'.format(shm.name))

for i in range(10):
    print('{} '.format(buf[i]), end='')

buf[4] = 200

print('')

I tested it under win10 64-bit system. The shared memory size obtained by process B is 4096, which is the size of a memory page. It can be seen that the minimum unit of memory allocated by the operating system is 4096 bytes, as follows,
Insert picture description here


summary

multiprocessing.shared_memory is very convenient for sharing memory between processes, but the python version is required to be 3.8 and above, and you need to pay attention to your python version when using it.
PS: These Linux C already existed...

Guess you like

Origin blog.csdn.net/whahu1989/article/details/109753804