0

In python multiprocessing, I have a dictionary of type Manager.dict(), the value of this dictionary is a list of strings, in order to to get the dictionary properly shared between processes, do I need to use the normal list or Manager.list() as the value type?

1 Answer 1

1

Manager.dict() will manage accesses into the arrays, no Manager.list() required. The dictionary returned by Manager.dict() will be a proxy to the real dictionary that lives on the manager.

The manager will let you read the lists, however it will not automatically persist any changes you make to them. For that, you'll have to set the values in your client code when you're done working with them. See the note on the bottom of the reference entry for details.

This will trigger a transmission of the entire list, which can be expensive. What's worse, it doesn't provide you with any safety guarantees regarding concurrent writing, so in two processes write the same list at the same time, only the second write will overwrite the first one.

I unfortunately can't suggest any alternative because I don't know enough about the tradeoffs you're able to make. Feel free to post another question, describing those, though!

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying. In my problem, I only have several key-value pairs in the dictionary, the key is a integer, the value is a list, I will distribute each key-value pair into each process, and only in one process(and always the same one) the value for some key can be modified, and only the values are modified, keys won't be added, deleted or modified. Welcome to give some advice.
Great! In that case it sounds like this solution is just what you need. If this answers your question, please accept this answer so it comes up at the top for future visitors.
do you think a lock is necessary if one process is changing the dictionary and other processes are reading the dictionary?
Correct, a lock would be necessary. You can always use a read-write lock to allow multiple reads and to control writes. Whether that is a good idea for performance depends entirely on your access patterns.
I am not sure your last comment is right or not. Here in this answer it says a lock is not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.