Using multiprocessing
on methods we get error stating self.methods cannot be pickled
.
To overcome this I used:
def _pickle_method(m):
if m.im_self is None:
return getattr, (m.im_class, m.im_func.func_name)
else:
return getattr, (m.im_self, m.im_func.func_name)
copy_reg.pickle(types.MethodType, _pickle_method)
Now I searched about this, but few questions are still unclear:
- Why can't
self.methods
be pickled? - How does
copy_reg.pickle()
work? How does it enable pickling ofself.methods
? - Are there any negatives of using this approach or are there any other, better, methods?
More Info:
I had a function in a class which used to do a request.get
and request.post
. To improve times , I used multiprocessing
over it.
This is the exact problem I faced.
Can't pickle <type 'instancemethod'> when using multiprocessing Pool.map()