Python共享内存阵列,无属性get_obj() [英] Python Shared Memory Array, no attribute get_obj()

查看:65
本文介绍了Python共享内存阵列,无属性get_obj()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多处理模块来处理numpy数组,并且在尝试我在此处遇到的一些代码时遇到了问题.具体来说,我是从一个numpy数组创建一个ctypes数组,然后尝试将ctypes数组返回到numpy数组.这是代码:

I am working on manipulating numpy arrays using the multiprocessing module and am running into an issue trying out some of the code I have run across here. Specifically, I am creating a ctypes array from a numpy array and then trying to return the ctypes array to a numpy array. Here is the code:

    shared_arr = multiprocessing.RawArray(_numpy_to_ctypes[array.dtype.type],array.size)

我不需要任何类型的同步锁,因此我正在使用RawArray.基于输入数组的dtype从字典中提取ctypes数据类型.那太好了.

I do not need any kind of synchronization lock, so I am using RawArray. The ctypes data type is pulled from a dictionary based on the dtype of the input array. That is working wonderfully.

    shared_arr = numpy.ctypeslib.as_array(shared_arr.get_obj())

在这里我得到一个堆栈跟踪说明:

Here I get a stack trace stating:

    AttributeError: 'c_double_Array_16154769' object has no attribute 'get_obj'

我还尝试过这篇文章中的以下内容,但会遇到相同的错误.

I have also tried the following from this post, but get an identical error.

    def tonumpyarray(shared_arr):
        return numpy.frombuffer(shared_arr.get_obj())  

我在运行python 2.6时遇到问题,不知道这是否是问题,共享变量名是否存在问题(我试图将内存使用率保持在尽可能低的水平,并且试图不复制numpy数组以及内存中的ctypes数组),或者其他一些东西,因为我只是在学习python的这一组件.

I am stuck running python 2.6 and do not know if that is the issue, if it is an issue with sharing the variable name (I am trying to keep memory usage as low as possible and am trying not to duplicate the numpy array and the ctypes array in memory), or something else as I am just learning about this component of python.

建议?

推荐答案

由于使用RawArray,因此它只是从共享内存分配的ctypes数组,没有包装的对象,因此不需要get_obj()方法即可获取包装的对象:

Since you use RawArray, it's just a ctypes array allocated from shared memory, There is no wrapped object, so you don't need get_obj() method to get the wrapped object:

>>> shared_arr = multiprocessing.RawArray("d",10)
>>> t = np.frombuffer(shared_arr, dtype=float)
>>> t[0] = 2
>>> shared_arr[0]
2.0

这篇关于Python共享内存阵列,无属性get_obj()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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