1

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]]
Markus
  • 9
  • 3
  • 1
    `row` and `L` refer to the same list. There's no need to do `L = row` after `row = L`. Assignments don't make copies. – Barmar Mar 06 '20 at 17:17
  • You might want to review Python variables in a tutorial such as https://realpython.com/python-variables/. I suspect you are bring some ideas from other languages that don't apply in Python. – hpaulj Mar 06 '20 at 20:41

1 Answers1

1

Look at this:

    lst1 = manager.list()
    lst1 = np.array(a).tolist()

The first statement makes lst1 a Manager list. But the second statement replaces it with an ordinary list, it's no longer a Manager.

You should create the list first, then make it managed.

    lst1 = np.array(a).tolist()
    lst1 = manager.list(lst1)
Barmar
  • 741,623
  • 53
  • 500
  • 612