I am using Python's multiprocessing
to create a parallel application. Processes need to share some data, for which I use a Manager
. However, I have some common functions which processes need to call and which need to access the data stored by the Manager
object. My question is whether I can avoid needing to pass the Manager
instance to these common functions as an argument and rather use it like a global. In other words, consider the following code:
import multiprocessing as mp
manager = mp.Manager()
global_dict = manager.dict(a=[0])
def add():
global_dict['a'] += [global_dict['a'][-1]+1]
def foo_parallel(var):
add()
print var
num_processes = 5
p = []
for i in range(num_processes):
p.append(mp.Process(target=foo_parallel,args=(global_dict,)))
[pi.start() for pi in p]
[pi.join() for pi in p]
This runs fine and returns p=[0,1,2,3,4,5]
on my machine. However, is this "good form"? Is this a good way to doing it, just as good as defining add(var)
and calling add(var)
instead?