'Pool within a Class in Python
I would like to use Pool within a class, but there seems to be a problem. My code is long, I created a small-demo variant to illustrated the problem. It would be great if you can give me a variant of the code below that works.
from multiprocessing import Pool
class SeriesInstance(object):
def __init__(self):
self.numbers = [1,2,3]
def F(self, x):
return x * x
def run(self):
p = Pool()
print p.map(self.F, self.numbers)
ins = SeriesInstance()
ins.run()
Outputs:
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 319, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
And then hangs.
Solution 1:[1]
It looks like because of the way the function gets passed to the worker threads (pickling) you can't use instance methods unfortunately. My first thought was to use lambdas, but it turns out the built in pickler can't serialize those either. The solution, sadly, is just to use a function in the global namespace. As suggested in other answers, you can use static methods and pass self to make it look more like an instance method.
from multiprocessing import Pool
from itertools import repeat
class SeriesInstance(object):
def __init__(self):
self.numbers = [1,2,3]
def run(self):
p = Pool()
squares = p.map(self.F, self.numbers)
multiples = p.starmap(self.G, zip(repeat(self), [2, 5, 10]))
return (squares, multiples)
@staticmethod
def F(x):
return x * x
@staticmethod
def G(self, m):
return [m *n for n in self.numbers]
if __name__ == '__main__':
print(SeriesInstance().run())
Solution 2:[2]
You can also use multiprocessing with static functions in the class.
Solution 3:[3]
You have an error, because pickle can't serialize instancemethod. So you should use this tiny workaround:
from itertools import repeat
from multiprocessing import Pool
class SeriesInstance:
def __init__(self):
self.numbers = [1, 2, 3]
def F(self, x):
return x * x
def run(self):
p = Pool()
print(list(p.starmap(SeriesInstance.F, zip(repeat(self), self.numbers))))
if __name__ == '__main__':
SeriesInstance().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | stardust |
Solution 3 |