[Numpy-discussion] numpy array sharing between processes? (and ctypes)

Ray S subscriber100 at rjs.org
Mon May 14 14:44:11 EDT 2007


While investigating ctypes and numpy for sharing, I saw that the 
example on
http://www.scipy.org/Cookbook/Ctypes#head-7def99d882618b52956c6334e08e085e297cb0c6
does not quite work. However, with numpy.version.version=='1.0b1', 
ActivePython 2.4.3 Build 12:


import numpy as N
from ctypes import *
x = N.zeros((3, 3), dtype=N.float64)
xdataptr = N.intp(x.__array_interface__['data'])[0]
y = (c_double * x.size).from_address(xdataptr)
y[0] = 123.
y[4] = 456.
y[8] = 789
print N.diag(x)

Works for me...

I can then do:
 >>> import  numpy.core.multiarray as MA
 >>> xBuf = MA.getbuffer(x)
 >>> z = MA.frombuffer(xBuf).reshape((3,3))
 >>> z
array([[ 123.,    0.,    0.],
        [   0.,  456.,    0.],
        [   0.,    0.,  789.]])
 >>> z[0,1] = 99
 >>> z
array([[ 123.,   99.,    0.],
        [   0.,  456.,    0.],
        [   0.,    0.,  789.]])
 >>> x
array([[ 123.,   99.,    0.],
        [   0.,  456.,    0.],
        [   0.,    0.,  789.]])
 >>> y[1]
99.0




More information about the NumPy-Discussion mailing list