Question

I am working on a distributed processing project. I need to send my composite data type to another peer, so i can use it in other peer to populate my TreeView structure. I have these two classes in my "tsk_composite" module, My composite data node :

class CTskNode(object):    
    def __init__(self, pData, pParent = None):
        self.mData = pData        
        self.mParent = pParent        
        self.mChildren = []   

My main class which sends the RootNode of tree structure :

class CTskComposite(object):
...
def getRootNode(self):
        self.getImagePartitions()
        return self.mParent  

def getImagePartitions(self):
    #I use this method to create my parentRoot with recursive function
    #it works good

I am sending my composite data using cPickle with this code :

def _composite(self, pArgs):
    self.mComposite = CTskComposite(pArgs)
    self.mCompositeRootNode = self.mComposite.getRootNode()
    return cPickle.dumps(self.mCompositeRootNode) 

When I receive the composite data in my second peer , it gives this error message :

ImportError: No module named tsk_composite

When I create an emty module with this name "tsk_composite" in my second peer, then it gives this error :

AttributeError: 'module' object has no attribute 'CTskNode'

And when i just wrote this line of code in the module in my second peer, it works perfectly.

class CTskNode(object):pass

Actually I don't need this module and class, How can i just import the module and class name to other peer by cpickle?

Was it helpful?

Solution

When Pickle serialize an object, it serialize a ditionnary that stores the attributes names/values and class name (full name, with modules pathes) not the method definitions.

When deserializing, it recreate an object with the same class and set the saved attributes to this object.

To re-create the object, pickle try to import the class in the client side, if it doesn't exists, it will raise the exception you saw.

To avoid this, I see 2 solutions :

  • you have to make the sources available to your client module, so when unpickling the objects it will found the sources.
  • you can re-create a dummy module (with the same path, class name and attributes) (if the only need of those objects is attributes hanlding)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top