I need to do run some computations to update object attributes. I want to use parallel computing since I need to update multiple objects' attributes, that is, I have multiple objects and I need to do the same computation for each one. The objects do not share information between them.
I am currently using a process pool with map
or a similar function, and the problem is that these processes copy the object, then do the computation, instead of just doing the computation directly using the original object. Is there any way around this?
As an example:
from multiprocessing import Pool
class A:
def __init__(self, init):
self.a = init
def func(self, b):
self.a = self.a + b
foo = A(2)
print(foo.a) # prints 2
p = Pool()
result = p.map(foo.func, (3,))
print(foo.a) #prints 2, should print 5
foo.func(3)
print(foo.a) #prints 5 as expected
question from:https://stackoverflow.com/questions/65931142/using-python-multiprocessing-to-run-object-computations