The output should be: [[], ['new element'], []] but is [[], [], []] just blank. Why?
from multiprocessing import Manager
if __name__ == '__main__':# Create a Manager object
manager = Manager()
# Create a shared list of lists
shared_list = manager.list([[], [], []])
# Append an element to the second index
element = "New Element"
shared_list[1].append(element)
# Print the updated shared list
print(shared_list)
>Solution :
Because you aren’t appending to the manager.list anywhere. You are retrieving a value, which copies a new object into your current process. That value is a list, when you then append to. But the manager.list has no way of knowing that. From the docs:
If standard (non-proxy)
listordictobjects are contained in a
referent, modifications to those mutable values will not be propagated
through the manager because the proxy has no way of knowing when the
values contained within are modified. However, storing a value in a
container proxy (which triggers a__setitem__on the proxy object)
does propagate through the manager and so to effectively modify such
an item, one could re-assign the modified value to the container proxy
So you could do:
inner = shared_list[1]
inner.append(element)
shared_list[1] = inner