M
M
Maxim2017-04-27 19:15:27
Python
Maxim, 2017-04-27 19:15:27

How to safely write to multiprocessing.Manager().dict()?

I want to use a dictionary like multiprocessing.Manager().dict() to write data in each running process. Each process must check if there is a value with the given key, if not, then create an entry. Do I need to use multiprocessing.Lock() before reading/writing or is this dictionary type thread safe? If the dictionary is thread-safe, is it best to create values ​​in one call via setdefault? Thank you.
UPDATE: I checked how setdefault works without lock(), it turned out that it is twice as slow as normal writing with lock(), what is the reason for such slow work?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question