Python Shared Memory Array, no attribute get_obj()

2024/10/1 21:32:56

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?

Answer

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
https://en.xdnf.cn/q/70919.html

Related Q&A

What is a qualified/unqualified name in Python?

In Python: what is a "qualified name" or "unqualified name"?Ive seen it mentioned a couple of times, but no explanation as to what it is.

Python code explanation for stationary distribution of a Markov chain

I have got this code: import numpy as np from scipy.linalg import eig transition_mat = np.matrix([[.95, .05, 0., 0.],\[0., 0.9, 0.09, 0.01],\[0., 0.05, 0.9, 0.05],\[0.8, 0., 0.05, 0.15]])S, U = eig(tr…

Detecting insertion/removal of USB input devices on Windows 10

I already have some working Python code to detect the insertion of some USB device types (from here).import wmiraw_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \W…

FastAPI as a Windows service

I am trying to run FastAPI as a windows service.Couldnt find any documentation or any article to run Uvicorn as a Windows service. I tried using NSSM as well but my windows service stops.

Why not use runserver for production at Django?

Everywhere i see that uWSGI and Gunicorn are recommended for production mode from everyone. However, there is a lot more suffering to operate with it, the python manage.py runserver is more faster, sim…

How to create decorator for lazy initialization of a property

I want to create a decorator that works like a property, only it calls the decorated function only once, and on subsequent calls always return the result of the first call. An example:def SomeClass(obj…

How to convert a 24-bit wav file to 16 or 32 bit files in python3

I am trying to make spectrograms of a bunch of .wav files so I can further analyze them(in python 3.6), however, I keep getting this nasty errorValueError: Unsupported bit depth: the wav file has 24-bi…

Get consistent Key error: \n [duplicate]

This question already has answers here:How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?(23 answers)Closed 8 years ago.When trying to run a script containin…

Cannot take the length of Shape with unknown rank

I have a neural network, from a tf.data data generator and a tf.keras model, as follows (a simplified version-because it would be too long):dataset = ...A tf.data.Dataset object that with the next_x me…

Pre-fill new functions in Eclipse and Pydev with docstring and Not Implemented exception

I am editing my Python source code with Eclipse and Pydev.I want to document all of my functions and raise a "Not Implemented" exception whenever a function have not yet been implemented. For…