[Numpy-discussion] numpy/Windows shared arrays between processes?

Robert Kern robert.kern@gmail....
Tue Oct 9 16:50:05 CDT 2007


Ray S wrote:

> Yes, a different methodology attempt. It would be interesting to know 
> anyway how to create a numpy array from an address; it's probably 
> buried in the undocumented C-API that I don't grok, and likely 
> frowned upon.

Make a dummy object that exposes the __array_interface__ attribute filled with
the appropriate information:

   http://numpy.scipy.org/array_interface.shtml

Something like the following should suffice (untested, though I've done similar
things with ctypes before):


import numpy

def fromaddress(address, dtype, shape, strides=None):
    """ Create a numpy array from an integer address, a dtype, a shape
    tuple, and possibly strides.
    """
    # Make sure our dtype is a dtype, not just "f" or whatever.
    dtype = numpy.dtype(dtype)

    class Dummy(object):
        pass
    d = Dummy()
    d.__array_interface__ = dict(
        data = (address, False),
        typestr = dtype.str,
        descr = dtype.descr,
        shape = shape,
        strides = strides,
        version = 3,
    )
    return numpy.asarray(d)

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the Numpy-discussion mailing list