I am trying to parallelize a code using class methods via multiprocessing. The basic structure is the following:
# from multiprocessing import Pool
from pathos.multiprocessing import ProcessingPool as Pool
class myclass(object):
def __init__(self):
#some code
def mymethod(self):
#more code
return another_instance_of_myclass
def myfunc(myinstance,args):
#some code
test=myinstance.mymethod()
#more code
return myresult #not an instance,just a number
p=Pool()
result = p.map(myfunc,listwithdata)
After this had failed with the normal multiprocessing, I became aware of the issues with Pickle and Multiprocessing, so I tried to solve it with multiprocessing.pathos. However, I am still getting
PicklingError: Can't pickle <type 'SwigPyObject'>: it's not found as__builtin__.SwigPyObjec
together with lots of errors from pickle.py. Apart from this practical problem, I don't quite understand why anything but the final result of myfunc is being pickled at all.
pathos
usesdill
, anddill
serializes classes differently than python'spickle
module does.pickle
serializes classes by reference.dill
(by default) serializes classes directly, and only optionally by reference.Not serializing by reference is much more flexible. However, in rare circumstances, working with references is better (as it appears to be the case when pickling something built on a
SwigPyObject
).I have been meaning (for ~2 years) to expose the
byref
flag to thedump
call inside ofpathos
, but have not done so yet. It should be a fairly simple edit to do so. I've just added a ticket to do so: https://github.com/uqfoundation/pathos/issues/58. While I'm at it, it should also be easy to open up replacement of thedump
andload
functions thatpathos
uses… that way you could use customized serializers (i.e. extend those thatdill
provides, or use some other serializer).In multiprocessing function serialization is needed for interprocess communication. Pickle does a poor job for this purpose, install dill via pip instead. Details (with a nice Star Trek example) can be found here: http://matthewrocklin.com/blog/work/2013/12/05/Parallelism-and-Serialization/