I'm trying to share a dictionary between threads of multiprocessing.Pool. However, I'm failed with current implementation. Test code:
#!/usr/bin/env python
import multiprocessing
success_map = {"1": []}
def run_(input):
global success_map
successes = success_map.get(input);
successes.append(0)
print success_map
pool = multiprocessing.Pool()
pool.map(run_, ["1"])
pool.close()
pool.join()
print success_map
The output is
{'1': [0]}
{'1': []}
It looks to me that worker(s) of multiprocessing.Pool() create copies of dictionary; that's why I can't see update when processing is finished. Am I right?
Note:
- I know it's possible to return value from
run_
function and get collected list as a result ofpool.map(run_, ["1"])
, but need to use global variable for current task - I'm aware of possible dataraces
- I've read somewhere that
global
statement isn't needed in that case but example works the same way without it - I've got the same result passing
[["1", success_map]]
topool.map
; without usage of global variable
Is it possible to share success_map
between threads in this example?
Related, but not an answer: Python Multiprocessing appending list