Question

I would like to store a 2-d array as shared memory array, so each process created by multiprocessing module can access the 2-d array. But it seems only vector is allowed. If I change the shape in the following example from (6,1) to (2,3), the example won't work and it says "TypeError: only length-1 arrays can be converted to Python scalars"

from multiprocessing import Process, Value, Array
import numpy as np

def f(n,a):
    a[:] = np.ones(shape=(6,1), dtype=float)[:]

if __name__ == '__main__':
    tmp =  np.zeros(shape=(6,1), dtype=float)
    print tmp

    arr = Array('f', tmp)

    p1 = Process(target=f, args=(1,arr))
    p1.start()
    p1.join()

    print 'done'
    print arr[:]
Was it helpful?

Solution

This is because mutiprocessing.Array is a wrapper around python's array type, not numpy.ndarray and python's array type doesn't support multiple dimensionality. Specifically, look at the documentation for the initializer:

A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, string, or iterable over elements of the appropriate type.

You've got an iterable (a multi-dimensional array), but it yields views/arrays, not elements of the appropriate type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top