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

Ray S subscriber100@rjs....
Tue Oct 9 16:52:36 CDT 2007


# numpyShared.py

import numpy as N
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

if __name__ == '__main__':
     ## test it
     ## produce a mish-mash of output on one shell screen
     import random
     import time
     import subprocess, sys
     import mmap

     if len(sys.argv)<2:
         pid = subprocess.Popen([r'C:\python24\python.exe', 
['numpyShared.py rjs100']]).pid
         ## the parent process
         a1 = arrSharedMemory((10000,), N.int32, tag='rjs100', 
access=mmap.ACCESS_WRITE)
         time.sleep(.7)
         now=time.time();
         while time.time()<now+3:
             tmp = a1[0]**.5
             a1[0]=random.random()*10
             #print '\np%d' % a1[0]
     else:
         tag = sys.argv[0][1]
         a2 = arrSharedMemory((10000,), N.int32, tag='rjs100', 
access=mmap.ACCESS_READ)
         now=time.time()
         while time.time()<now+3:
             tmp = a2[0]**.5
             #print '\nc%d' % a2[0]
     



More information about the Numpy-discussion mailing list