[Numpy-discussion] numpy version and numpy.asarray behavior issue

Ray S subscriber100 at rjs.org
Mon Oct 29 16:54:49 EDT 2007


I just installed 1.0.3.1 on top of Enthought's and asarray() works.

But...
Although the creation of an array from an address via a Dummy class 
is kosher in one process (as in the previous attachment), it fails 
across processes - the array is created, but gives a "Python has 
generated errors" window if the second process even attempts to read.

It can seem to work across processes with mmap.mmap() and tags (used 
in Windows)

def arrSharedMemory(shape, dtype, tag="PySharedMemory", access=None):
     ## Windows only !  share memory between different
     ## processes if same `tag` is used.
     itemsize = N.dtype(dtype).itemsize
     count = N.product(shape)
     size =  count * itemsize
     import mmap
     sharedmem = mmap.mmap(0, size, tag, access)
     a=N.frombuffer(sharedmem, dtype, count)
     a.shape = shape
     return a

I guess I'll use mmap unless someone can point out otherwise....

Thanks,
Ray
-------------- next part --------------
""" nFromAddress.py
"""

##Numeric example, with address kludge
import Numeric, numpy, ctypes, subprocess
from time import clock, sleep

"""a = Numeric.zeros((4), Numeric.Int16)
nAddress = int(repr(.__copy__).split()[-1][:-1], 16)
tmp=(ctypes.c_long*1)(0)
ctypes.memmove(tmp, nAddress+8, 4)
nAddress = tmp[0]
"""

a = numpy.zeros(4, numpy.int16)
nAddress = a.__array_interface__['data'][0]

print nAddress
pid = subprocess.Popen(
    [r'C:\python24\python.exe', 
     ['nFromAddress2.py '+str(nAddress)]
    ]).pid

while clock()<5:
    sleep(.1)
    if a[0]!=0: ## wait for a change...
        print a0[0]

-------------- next part --------------
""" nFromAddress.py
"""

import numpy
import time, sys
def fromaddress(address, dtype, shape, strides=None):
    """ Create a numpy array from an integer address, a dtype 
    or dtype string, 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)


nAddress = sys.argv[1]
print 'recvd addr', nAddress
a3 = fromaddress(nAddress, numpy.int16, (4,))

## any of the following cause a Python/Windows error on access
print a3
#a3[0] = 5


More information about the NumPy-Discussion mailing list