Constructing an array from memory address

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 10 19:36:49 CDT 2006


Daniel Drake wrote:

>Hi,
>
>I have an area of memory which is shared between processes (it is
>actually a shared memory segment). The address of this memory is stored
>in a python long variable, which I pass to various custom C/C++ python
>modules.
>
>I would like to construct a numpy array in this area. Is there any way I
>can tell numpy to use a specific address (stored as a long) to use as
>storage for the array? 
>

This functionality is already available.   There are two ways to do it. 

1) Create an object with an __array_interface__ attribute that returns a 
dictionary with shape, typestr, and data fields.   The data field should 
return the tuple  (memory-address-as-long, 
True-if-read-only-otherwise-False).

Passing this object into the asarray constructor will use the memory given

2) Use the asbuffer function in numpy.core.multiarray  (it's not exposed 
to numpy yet) to create a buffer object from the memory address and then 
use frombuffer to create an array from the memory.


Good luck,

-Travis


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV




More information about the Numpy-discussion mailing list