7

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?

1
  • I should add that I really am not trying to get back to a numpy array, simply return a view of the ctypes array that I can write out as a numpy array (if I am understanding the order of operations properly). Commented Jul 27, 2012 at 18:41

1 Answer 1

12

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.