I am trying to use python multiprocessing
module to process a large set of urls for which I am creating worker processes using a multiprocessing.Pool
object as shown below.
from multiprocessing import Pool, TimeoutError, cpu_count
class MyClass:
def square(self, x):
return x*x
@staticmethod
def getNumbers():
return range(10)
def calculate(self):
pool = Pool(processes=min(cpu_count(),8))
results = [pool.apply(self.square,(i,)) for i in self.getNumbers()]
pool.close()
pool.join()
for result in results:
print result
if __name__ == '__main__':
instance = MyClass()
instance.calculate()
However the above piece of code results in a pickling error as follows:
Traceback (most recent call last):
File "multi.py", line 24, in <module>
instance.calculate()
File "multi.py", line 15, in calculate
results = [pool.apply(self.square,(i,)) for i in self.getNumbers()]
File "/usr/lib/python2.7/multiprocessing/pool.py", line 244, in apply
return self.apply_async(func, args, kwds).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
NOTE A similar question has been asked previously on SO by someone else but remains unanswered: cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
EDIT Gave a better sample code example