Python getting shared memory: size is not consistent

Viewed 151

Process 1:

shm=multiprocessing.shared_memory.SharedMemory(name="shm", create=True, size=10000)
print(shm.size)

Prints 10000

Process 2:

shm=multiprocessing.shared_memory.SharedMemory(name="shm")
print(shm.size)

Prints 12288

The problem is that I'm trying to use the buffer to back a numpy array. Then numpy complains it cannot reshape() the array because it is not the same size.

1 Answers

shared memory is rounded to the next page size, which is in your case 3 * 4096. You have to slice the buffer to the correct size

shm = multiprocessing.shared_memory.SharedMemory(name="shm")
buffer = shm.buf[:10000]
Related