Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Shared Memory Array, no attribute get_obj()

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)

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())  

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.

Suggestions?

like image 710
Jzl5325 Avatar asked Jul 27 '12 18:07

Jzl5325


1 Answers

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
like image 61
HYRY Avatar answered Nov 01 '22 05:11

HYRY