简体   繁体   中英

Python getting shared memory: size is not consistent

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.

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]

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