I found the following example on this website somewhere:
import multiprocessing
import ctypes
import numpy as np
shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(10, 10)
# No copy was made
assert shared_array.base.base is shared_array_base.get_obj()
# Parallel processing
def my_func(i, def_param=shared_array):
shared_array[i,:] = i
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
pool.map(my_func, range(10))
print shared_array
The above code works fine, but if I want to add an array to the shared array, something like shared_array += some_other_array (instead of the above shared_array[i,;] = i) I get
local variable 'shared_array' referenced before assignment
Any ideas why I cannot do that?