I have an object with two attributes: a dict and an int. When I modify the object using a forked process via multiprocessing.Pool, I get back the object with it the modified int attribute, but the dict isn't modified. Why is that?
from multiprocessing import Pool def fork(): someObject = SomeClass() for i in range(10): someObject.method(i) print("in fork, someObject has dct=%s and nbr=%i" % (someObject.dct, someObject.nbr)) return someObject def test(): pool = Pool(processes=1) result = pool.apply(func=fork) print("in main, someObject has dct=%s and nbr=%i" % (result.dct, result.nbr)) class SomeClass(object): dct = {} nbr = 0 def method(self, nbr): self.dct[nbr]=nbr self.nbr+=nbr if __name__=='__main__': test() Output:
in fork, someObject has dct={0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9} and nbr=45
in main, someObject has dct={} and nbr=45