I am trying to use Python\'s pathos to designate computations into separate processes in order to accelerate it with multicore processor. My code is organized like:
Here's how I go about this - I put the function to be run in parallel outside the class and pass the object as an arg while calling pool.map. Then, I return the object to be reassigned.
from pathos.multiprocessing import ProcessingPool
def boo(args):
b, things = args
for thing in things:
b.sum += b.foo(thing)
return [b, b.sum]
class Bar:
def __init__(self):
self.sum = 0
def foo(self, name):
return len(str(name))
pool = ProcessingPool(2)
b1 = Bar()
b2 = Bar()
print(b1, b2)
results = pool.map(boo, [[b1, [12,3,456]],[b2, ['a','b','cde']]])
b1, b1s = results[0]
b2, b2s = results[1]
print(b1,b1s,b1.sum)
print(b2, b2s, b2.sum)
Output:
(<__main__.Bar instance at 0x10b341518>, <__main__.Bar instance at 0x10b341560>)
(<__main__.Bar instance at 0x10b3504d0>, 6, 6)
(<__main__.Bar instance at 0x10b350560>, 5, 5)
Note that b1 and b2 are no longer the same as what they were before calling map
because copies of them were made to be passed, as described by @Mike McKerns. However, the values of all their attributes are intact because they were passed, returned and reassigned.