I am using multiprocessing.Pool to distribute the work of a method on several processors. When I add something to a dictionary, it is lost after the method is executed. Why is that? And how to circumvent it?
from multiprocessing import Pool
class Agent:
def __init__(self):
self.test_dict = {}
def apply(self, num):
# something very processor intensive here
self.test_dict[num] = num
print 'inside ', self.test_dict
def F(x):
agent, i = x
return agent.apply(i)
class SeriesInstance(object):
def __init__(self):
self.agent = Agent()
self.F = F
def run(self):
p = Pool()
for i in range(5):
out = p.map(F, [(self.agent, i),])
print 'outside', self.agent.test_dict
p.close()
p.join()
return out
if __name__ == '__main__':
SeriesInstance().run()
the output is this, but outside should be equal to inside
inside {0: 0}
outside {}
inside {1: 1}
outside {}
inside {2: 2}
outside {}
inside {3: 3}
outside {}
inside {4: 4}
outside {}