Hello I am trying define values of a dictionary in parallel using multiprocessing. When the function f() is called outside “pool” the dictionary value is set correctly. In the pool call however it fails.
What am I doing wrong? Thanks.
from multiprocessing import Pool
hits={}
def f(x):
hits[x] = x #this will involve a complex operation
f('000')
print hits['000']
if __name__ == '__main__':
pool = Pool(processes=2)
inputs = ['a','b','c']
result = pool.map(f, inputs)
print hits['a']
You spawn a bunch of subprocesses which inherit the current value of
hits. Each subprocess get its own copy ofhitsand modifies it, then exits, dropping the process-local copy ofhits.The intended use of
multiprocessing.Pool.map()is to use its return value, which you are ignoring. Each process could return its copy ofhits, and you finally join these copies.