Search code examples
pythonclassconcurrent.futures

How to run Python's ProcessPoolExexcutor with a class function within the class itself?


Hope my title makes sense, if not here's an example of what I am talking about

from concurrent.futures import ProcessPoolExecutor


class App:
    def __init__(self):
        pass

    def __funct(self, a, b):
        return a * b

    def doing_func_in_parallel(self, list_a, list_b):
        process_executors = ProcessPoolExecutor()
        process_futures = []
        for a, b in zip(list_a, list_b):
            process_future = process_executors.submit(self.__funct, a=a, b=b)

            process_futures.append(process_future)

        list_result = []
        for future in process_futures:
            list_result.append(future.result())
        return list_result


if __name__ == '__main__':
    test_app = App()
    list_a = [1, 2, 3]
    list_b = [1, 2, 3]
    print(test_app.doing_func_in_parallel(list_a, list_b))

When I try to run it this I get:

AttributeError: 'App' object has no attribute '__funct'

Can I not call ProcessPoolExecutor running a method within the same class? Is there any work around that?


Solution

  • This error has to do with Python's naming convention.

    Double underscore will mangle the attribute names of a class to avoid conflicts of attribute names between classes. Python will automatically add "_ClassName" to the front of the attribute name which has a double underscore in front of it. Read more

    To fix your application, just rename your method to _funct.