David
David

Reputation: 75

multithreading/processing in Python

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']

Upvotes: 4

Views: 289

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601361

You spawn a bunch of subprocesses which inherit the current value of hits. Each subprocess get its own copy of hits and modifies it, then exits, dropping the process-local copy of hits.

The intended use of multiprocessing.Pool.map() is to use its return value, which you are ignoring. Each process could return its copy of hits, and you finally join these copies.

Upvotes: 7

Related Questions