I am wondering if I create a dict through multiprocessing.Mananger(), will its value be locked automatically when a processing is manipulating it, or I should write lock.acquire/release explicitly?
Previously, I wrote lock.acquire/release inside the function explicitly, however, it seems my code suffer from the problem of dead lock. It is strange since I think there is only one lock in my code. Therefore I am wondering if manager.dict will give another lock automatically. When I delete the lock.acquire/release, the code works fine. But I am not sure if the dict is correct.
import multiprocessing as mp
from functools import partial
def function(file_name, d, lock):
key, value = read_files(file_name)
#lock.acquire()
if (key not in d):
d[key] = []
tmp = d[key]
tmp.append(value)
d[key] = tmp
#lock.release()
if __name__ == '__main__':
manager = mp.Manager()
d = manager.dict()
lock = manager.Lock()
partial_function = partial(function, d=d, lock=lock)
pool = mp.Pool(10)
pool.map(partial_function, files) #files is a predefined list
pool.close()
pool.join()
Some of the related questions are listed below, but they seems to contradict to each other.