使用 cPickle 通过 XML-RPC 发送复合数据类型
我正在从事一个分布式处理项目。 我需要将复合数据类型发送到另一个对等点,以便我可以在其他对等点中使用它来填充我的 TreeView 结构。 我的“tsk_composite”模块中有这两个类, 我的复合数据节点:
class CTskNode(object):
def __init__(self, pData, pParent = None):
self.mData = pData
self.mParent = pParent
self.mChildren = []
发送树结构 RootNode 的主类:
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
我使用 cPickle 发送复合数据并使用以下代码:
def _composite(self, pArgs):
self.mComposite = CTskComposite(pArgs)
self.mCompositeRootNode = self.mComposite.getRootNode()
return cPickle.dumps(self.mCompositeRootNode)
当我在第二个对等点中接收复合数据时,它会给出以下错误消息:
ImportError: No module named tsk_composite
当我创建空值时在我的第二个对等点中具有此名称“tsk_composite”的模块,然后它给出此错误:
AttributeError: 'module' object has no attribute 'CTskNode'
当我刚刚在第二个对等点的模块中编写这行代码时,它工作得很好。
class CTskNode(object):pass
实际上我不需要这个模块和类,我如何通过 cpickle 将模块和类名导入到其他同行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 Pickle 序列化一个对象时,它会序列化一个字典,该字典存储属性名称/值和类名称(全名,带有模块路径)而不是方法定义。
反序列化时,它会重新创建一个具有相同类的对象,并将保存的属性设置给该对象。
要重新创建对象,pickle 尝试在客户端导入该类,如果它不存在,它将引发您看到的异常。
为了避免这种情况,我看到了 2 个解决方案:
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 :