I would like to change a numpy array in multiprocessing fuunction. So I converted it to manager.list and gave to the subprocess. The changes (append) which are made in the subprocess did not return to the original list. It works with another list with which I execute the same changes except for the fact that base list does not come from a converted array.
I've expected an [8] in row1 after processing, but it didn't change. Can anybody help? Thank you very much.
Here is the code:
from multiprocessing import Process, Manager
import numpy as np
def f(L, L1):
row = L
row.append([4])
L = row
row1 = L1
row1.append([8])
L1 = row1
if __name__ == '__main__':
manager = Manager()
lst = manager.list()
lst.append([1])
lst.append([2])
lst.append([3])
print('lst main before: ', lst)
a = np.array([[5],[6]])
lst1 = manager.list()
lst1 = np.array(a).tolist()
lst1.append([7])
print('lst1 main before: ', lst1)
p = Process(target=f, args=(lst,lst1))
p.start()
p.join()
print('lst main after: ', lst)
print('lst1 main after: ', lst1)
Output
lst main before: [[1], [2], [3]]
lst1 main before: [[5], [6], [7]]
lst main after: [[1], [2], [3], [4]]
lst1 main after: [[5], [6], [7]]