Reputation: 2169
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:
run_
function and get collected list as a result of pool.map(run_, ["1"])
, but need to use global variable for current taskglobal
statement isn't needed in that case but example works the same way without it[["1", success_map]]
to pool.map
; without usage of global variableIs it possible to share success_map
between threads in this example?
Related, but not an answer: Python Multiprocessing appending list
Upvotes: 1
Views: 714
Reputation: 43563
Multiprocessing uses separate processes, not threads. These processes do not share all memory like threads do.
To share data between processes you could use e.g. multiprocessing.Value
or multiprocessing.Array
. But be aware that in some cicrumstances you'll need an extra Lock.
Using a Manager
object you can share most data types (it must be pickle-able, I think). And it's slower that shared memory.
Or you could create a multiprocessing.Pipe
to exchange data between processes.
Notes:
Upvotes: 4