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

Ray S subscriber100@rjs....
Mon Oct 29 15:54:49 CDT 2007

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

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....

-------------- 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)
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(
     ['nFromAddress2.py '+str(nAddress)]

while clock()<5:
    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):
    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