Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't i append to manager.list()?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) list or dict objects 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading