How to parallelize a class method in python?

1k Views Asked by At

I have a class with a method foo(), and a dictionary with a bunch of objects of that class.

How can I parallelize the execution of foo() in my collection of objects?

class MyClass:
    def __init__(self,a):
        self.a = a
    def foo():
        self.a+=1
        
if __name__ == "__main__":
    
    #get a dictionary full of independent objects
    dic={}
    for kk in range(10):
        dic[kk]=MyClass(kk)
    
    #execute foo on each object. 
    #How to do it in a parallel way?
    for myObject in dic.values():
        myObject.foo()

pd: this is probably a silly question, really simple in other programing languages, but I did google on the internet and did not find any straight forward solution.

1

There are 1 best solutions below

6
On

You didn't provide a code sample so I'll give you some pseudo code but this should give you an idea of how to solve it:

from multiprocessing import Pool

class myClass:
    def foo(x):
        # Do your processing here
        return x

if __name__ == "__main__":

    # Your dictionary here
    some_dict = {"key1": {}, "key2": {}}

    # Create an instance of your class
    my_class = MyClass()

    # Create a pool of workers
    with Pool(5) as p:
        p.map(my_class.foo, [obj for obj in some_dict.values()])