Python multithreading / processing

Hello, I am trying to determine dictionary values ​​in parallel using multiprocessing. When the f () function is called outside the "pool", the dictionary value is set correctly. However, in a pool call, 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'] 
+4
source share
1 answer

You create a bunch of subprocesses that inherit the current hits value. Each subprocess receives its own copy of hits and modifies it, and then terminates by discarding the local-local copy of the hits process.

The intended use of multiprocessing.Pool.map() is to use its return value, which you ignore. Each process can return a copy of hits , and you will finally join these copies.

+7
source

All Articles