pythonpython-3.xpython-multiprocessingmultiprocessing-manager

Multiprocessing manager's list does not hold values after re-initializaing


I have this situation where a multiprocess manager's list needs to be re-initialized to [] to make it blank list, but after doing so it does not modify the list anymore.

It does modify it locally inside definition doesnt hold those values after the process exits, but instead shows older values which it held till until it was re-initialized. Why is this happening and how it can be fixed?

import multiprocessing
import random

def mydef(my_list):
    data_list = [3,2,2,6,7,4,2,5,6,9,3,2]
    for n in data_list:
        if n == 7:
            my_list = []
        else:
            my_list.append(n)
        print(my_list)


m = multiprocessing.Manager()

m_list = m.list()
p = multiprocessing.Pool(processes=4)
p.apply_async(mydef, (m_list,))
p.close()
p.join()

print(f'\nThe final list is {m_list}')

Output of this is

[3]
[3, 2]
[3, 2, 2]
[3, 2, 2, 6]
[]
[4]
[4, 2]
[4, 2, 5]
[4, 2, 5, 6]
[4, 2, 5, 6, 9]
[4, 2, 5, 6, 9, 3]
[4, 2, 5, 6, 9, 3, 2]

The final list is [3, 2, 2, 6]

Solution

  • When you do:

    my_list = []
    

    You dissociated the function's my_list from global my_list, try :

    my_list[:] = []