I have a dictionary of objects, and I would like to populate this dictionary using the multiprocessing package. This bit of code runs "Run" many times in parallel.
Data=dict()
for i in range:
Data[i]=dataobj(i) #dataobj is a class I have defined elsewhere
proc=Process(target=Run, args=(i, Data[i]))
proc.start()
Where "Run" does some simulations, and saves the output in the dataobj object
def Run(i, out):
[...some code to run simulations....]
out.extract(file)
My code creates a dictionary of objects, and then modifies the objects in that dictionary in parallel. Is this possible, or do I need to acquire a lock every time I modify an object in the shared dictionary?
basically, as you're using multiprocessing, then your processes share copies of the original dictionary of objects, and thus populate different ones. What the multiprocessing package handles for you, is messaging of python objects between processes to make things less painful.
A good design for your problem is to have the main process handling populating the dictionary, and have its children processes handle the work. Then use a queue to exchange data between the children processes and the master process.
As a general design idea, here's something that could be done:
though, take it as only a design idea, as this code has a few design flaws, which will get naturally solved when working with real data and processing functions.