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
multiprocessinguses processes which have separated memory and it has to send data to processes (usingpickle). You may try to use multiprocessing.shared_memory but I never used it.threadsuses shared memory.(3,)in the example + theinitvalue)... but this is not a parallel operation, i.e. you can't doNparallel additions and have a sum ofNelements.return self.aand displayresult- orreturn selfand displayresult.a