简体   繁体   English

Pyro4:反序列化protobuf类

[英]Pyro4: Deserializing protobuf class

I am completely new to Pyro4. 我对Pyro4完全陌生。 I wish to serve a python class which includes an attribute which is an instance of a protobuf object compiled to a python class. 我希望提供一个python类,其中包含一个属性,该属性是已编译为python类的protobuf对象的实例。

import sys
import os
import Pyro4
import MPB_pb2 as mpb  # My Protobuf class

@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class MyWrappedProtobuf(object):
    def __init__(self):
        self.setup_MyProtobuf()

    def setup_MyProtobuf(self):
        self.__MyProtobuf = mpb.MyProtobufMessage()

    @property
    def MyProtobuf(self):
        return self.__MyProtobuf

After setting the server running, I create a proxy of the served object, and query the MyProtobuf property: 设置服务器运行后,我创建服务对象的代理,并查询MyProtobuf属性:

u = Pyro4.Proxy('PYRONAME:{}'.format(<server name>)).MyProtobuf

What I get back is a serialized object: 我得到的是一个序列化对象:

>>>u
{'serialized': '\n$\n\x05world\x12\x1b\t\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x00\x00\x00\x00\x00'}

I understand from the documents ( https://pythonhosted.org/Pyro4/clientcode.html#changing-the-way-your-custom-classes-are-de-serialized ) that "By default, custom classes are serialized into a dict. They are not deserialized back into instances of your custom class." 我从文档( https://pythonhosted.org/Pyro4/clientcode.html#changing-the-way-your-custom-classes-are-de-serialized )中了解到,“默认情况下,自定义类会序列化为字典它们不会反序列化为您的自定义类的实例。”

I am looking for instruction, or an example describing how I can go about turning this serialized dictionary back into my class, including the protobuf message instance that class contains. 我正在寻找说明,或寻找一个示例说明如何将这个序列化的字典转回我的类,包括该类包含的protobuf消息实例。

It seems like this must be a common, and trivial operation, and yet I can't find a good example to show me, and the documentation is not providing explicit help that I can see. 看来这肯定是一个普通且微不足道的操作,但是我找不到很好的例子向我展示,并且文档没有提供我可以看到的明确帮助。

Thank you! 谢谢!

By default, Pyro uses the Serpent serializer, which in turn uses python's primitive types to serialize stuff into. 默认情况下,Pyro使用Serpent序列化器,后者又使用python的原始类型将内容序列化为。 Usually this will be a dictionary. 通常这将是一本字典。 If it's a class it doesn't readily recognize or the class doesn't have a "suitable" serialization method defined, it will fall back to a special dictionary form such as this: 如果它不是一个容易识别的类,或者该类没有定义“合适的”序列化方法,则它将退回到特殊的字典形式,例如:

{
    "__class__":   "pakcage.Classname"
    "property1":   "value1",
    ...
}

You're not seeing this in your serialized form. 您没有以序列化的形式看到它。 Which means it wasn't Pyro (or Serpent, rather) that serialized it for you. 这意味着不是Pyro(或Serpent,而是蛇)为您序列化了它。 What happened (I think) is that a Protobuf object defines a __getstate__() method that returns the serialized form of the protobuf object that you're seeing. 发生的事情(我认为)是Protobuf对象定义了一个__getstate __()方法,该方法返回您所看到的protobuf对象的序列化形式。 The reverse of this is __setstate__(...) (these methods are 'borrowed' from Python's built in pickle serializer mechanism). 与此相反的是__setstate __(...)(这些方法是从Python的内置pickle序列化器机制中“借用”的)。 I don't have experience with protobufs but my guess is that a simple: 我没有protobufs的经验,但是我的猜测很简单:

u = proxy.MyProtoBuf
message = mpb.MyProtobufMessage()   # create empty protobuf object (??)
message.__setstate__(u)

will do the trick. 会成功的 I advise you to look into protobuf's documentation about how their objects are serialized (you may have to search for how they are pickled). 我建议您仔细阅读protobuf的文档,以了解如何序列化它们的对象(您可能必须搜索它们的腌制方式)。 Bottom line; 底线; this is not something Pyro (or serpent) has control over. 这不是火焰兵(或蛇)所能控制的。

It was indeed serializing from protobuf which I was seeing. 确实是从我所见的protobuf开始序列化的。 Once that confucion was sorted, I found that the serialized results were unicode. 对孔夫子进行分类后,我发现序列化的结果是unicode。 In order for me to be able to get protobuf to deserialize the results, I needed to encode it as pure ascii (using <result>.encode('utf-8') ). 为了使protobuf可以反序列化结果,我需要将其编码为纯ascii(使用<result>.encode('utf-8') )。 I'm not sure why it's returning unicode which it won't thereafter turn around and accept. 我不确定为什么它会返回unicode,此后它不会转身接受。 However, I have a working solution. 但是,我有一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM