从共享内存的数组缓冲区中形成numpy数组(多重处理)失败 [英] Forming numpy array from array buffer from shared memory (multiprocessing) fails

查看:105
本文介绍了从共享内存的数组缓冲区中形成numpy数组(多重处理)失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在两个进程之间的共享内存中有一个多维数组.我试图做一个简单的示例,将其发送给另一个进程,将它重新整形为[[1, 2, 3], [4, 5, 6], [7, 8, 9]]而不占用额外的内存.

I need to have a multidimensional array in a shared memory between two processes. I'm trying to make a simple example that works: I send [1, 2, 3, 4, 5, 6, 7, 8, 9] to the other process, which reshapes it into [[1, 2, 3], [4, 5, 6], [7, 8, 9]] without taking additional memory.

import multiprocessing
import ctypes
import numpy


def f(array):
    nmp = numpy.frombuffer(array.get_obj(), dtype=int)
    b = nmp.reshape((3, 3))


if __name__ == '__main__':
    time_array = []
    import common_lib
    common_lib.time_usage(time_array)
    arr = multiprocessing.Array(ctypes.c_int, [1,2,3,4,5,6,7,8,9])
    p = multiprocessing.Process(target=f, args=(arr,))
    p.start()
    p.join()

我完全按照手册中的方法进行操作.但是函数frombuffer给出此错误:

I did exactly as was in the manuals. But the function frombuffer gives this error:

ValueError:缓冲区大小必须是元素大小的倍数

ValueError: buffer size must be a multiple of element size

推荐答案

numpy数组的dtype必须明确设置为32位整数.

The dtype for the numpy array needs to be explicitly set as a 32-bit integer.

nmp = numpy.frombuffer(array.get_obj(), dtype="int32")

如果您使用的是64位计算机,则可能是您试图将32位ctypes数组转换为64位numpy数组.

If you are on a 64-bit machine, it is likely that you were trying to cast the 32-bit ctypes array as a 64-bit numpy array.

这篇关于从共享内存的数组缓冲区中形成numpy数组(多重处理)失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆