I have a simulation that consists of a list of objects. I'd like to call a method on all of those objects in parallel, since none of them depends on the other, using a thread pool. You can't pickle a method, so I was thinking of using a wrapper function with a side effect to do something like the following:
from multiprocessing import Pool
class subcl:
def __init__(self):
self.counter=1
return
def increment(self):
self.counter+=1
return
def wrapper(targ):
targ.increment()
return
class sim:
def __init__(self):
self.world=[subcl(),subcl(),subcl(),subcl()]
def run(self):
if __name__=='__main__':
p=Pool()
p.map(wrapper,self.world)
a=sim()
a.run()
print a.world[1].counter #should be 2
However, the function call doesn't have the intended side effect on the actual objects in the array. Is there a way to handle this simply with a thread pool and map, or do I have to do everything in terms of raw function calls and tuples/lists/dicts (or get more elaborate with multiprocessing or some other parallelism library)?