Search code examples
pythonpython-3.xmultiprocessingpython-multiprocessing

Why does this error occur when using pool.map in Python with multiprocesssing module


I am using the multiprocessing module in Python to render certain operations parallel. The function I want to parallelize is a method inside a class that I instantiate at a certain point in the program. The function returns an object of a class called History and doesn't take any arguments.

I want to use the map method of the Pool object in the multiprocessing module so I can create many different processes at the same time, I create a dummy list of numbers to take as "input" even though the method doesn't need it, and use:

Pool.map(function,numbers)

However this always raises the issue: History object is not callable. Why does it say this? I want to return a lsit of history objects, why does it say I am trying to call them?

Code that calls method:

            p = Pool(process_count)
            numbers = [1] * process_count
            results = p.map(self.function(), numbers)
            p.join()
            p.close()

Solution

  • Pool.map is a method which calls the supplied function on each of the element of the iterable parallel. When you supply map with self.function() as the first argument, you are not giving it a function but the result of the function - in your case History. Then map tries to call History resulting the error.