0

Is it possible to lock a column or row of a ctypes array using get_lock()?

Something like:

lock = mp.Lock()
cArray = mp.Array(ctypes.c_int, 100*4, lock = lock)
numpyArray = np.frombuffer(cArray.get_obj())
numpyArray.shape = (100,4)

#In a function later called via multiprocessing.Process - for example
numpyArray[:,0].get_lock() #Also could be numpyArray.T[0].get_lock()

I know that it is possible to lock the entire ctypes array, but what just a segment of it. I know the size of the array and it will remain constant so I could compute the offsets in the ctypes array, but wonder - Can one leverage the ease of use of numpy's slicing with a multiprocessing lock?

Jzl5325
  • 3,898
  • 8
  • 42
  • 62
  • related: [Use numpy array in shared memory for multiprocessing](http://stackoverflow.com/questions/7894791/use-numpy-array-in-shared-memory-for-multiprocessing) (look at `f()` function in my answer) – jfs Jan 21 '13 at 01:01

1 Answers1

1

If you have a lock you can lock any code block:

with lock:
    numpyArray[:,0] = 1
    a += 2

Numpy array doesn't have an associated multiprocessing lock. To get it; you could call cArray.get_lock() or just pass as a variable lock.

jfs
  • 399,953
  • 195
  • 994
  • 1,670