简体   繁体   English

Python获取共享内存:大小不一致

[英]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打印 10000

Process 2:过程2:

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

Prints 12288打印 12288

The problem is that I'm trying to use the buffer to back a numpy array.问题是我正在尝试使用缓冲区来支持 numpy 数组。 Then numpy complains it cannot reshape() the array because it is not the same size.然后 numpy 抱怨它无法reshape()数组,因为它的大小不同。

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共享内存四舍五入到下一页大小,在您的情况下为 3 * 4096。您必须将缓冲区切片到正确的大小

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

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

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