I’m just starting to work with python multiprocessing.
The problem that I’m facing with my code can be summarized in the following way.
Basically, I am trying to have a 2D array being accessed and modified by several different processes.
I found a similar question here on StackOverflow from a year ago by Alam (How to create a shared 2D array in python multiprocessing) but the answer to the issue was not clearly given.
Here is a code snippet, similar to the one presented by Alam, to help the discussion.
import multiprocessing as mp import numpy as np import ctypes as c n = 2 m = 3 def addData(array, process_number): n,m = np.shape(array) i=0 for nn in range(n): for mm in range(m): array[nn][mm] += i i=i+1 print("Array after process " + str(process_number)) print(array) if __name__=='__main__': mp_arr=mp.Array('i', n*m) arr = np.frombuffer(mp_arr.get_obj(),c.c_int) u = arr.reshape((n, m)) print("Array at the inital state: ") print(u) p1=mp.Process(target=addData,args=(u,1)) p2=mp.Process(target=addData,args=(u,2)) p1.start() p2.start() p1.join() p2.join() print("Array at the final state: ") print(u)
Here is the output:
As you can see, the final output of the 2D array is still [[0,0,0], [0,0,0]]
and I would like it to be [[0,2,4], [6, 8, 10]].
Source: Python Questions