首页 > 解决方案 > 异常序列化消息

问题描述

我在 grpc 中使用消息请求流,如下所示:

def Post(self, request_iterator, context):
        print("Post")
        for request in request_iterator:
            print("Request")
            if hasattr(request , "type"):
                print("Has type")

使用以下原型:

syntax ="proto3";

package nbsb;

message SDF {
    Thing thing = 1;
    repeated Object objects = 2;
    string type = 3;
}

message Property {
    string name = 1;
    string value = 2;
}
message Thing {
    string id = 1;
    string name = 2;
}
message Object {
    repeated string idObject = 1;
    string name = 2;
    repeated Property properties = 3;
}

message Response {
    repeated string id = 1;
    string value = 2;
}

service Connector{
    rpc Get(SDF) returns (Response){}
    rpc Put(SDF) returns (Response){}
    rpc Post(stream SDF) returns (stream SDF){}
}

期望的输出应该是正确的消息序列化和反序列化。输出如下:

Post
<grpc._server._RequestIterator object at 0x000002CA1CFE5100>
ERROR:grpc._common:Exception serializing message!
Traceback (most recent call last):
  File "C:\Python\Python3\lib\site-packages\grpc\_common.py", line 83, in _transform
    return transformer(message)
  File "C:\Python\Python3\lib\site-packages\google\protobuf\internal\python_message.py", line 1082, in SerializeToString
    if not self.IsInitialized():
AttributeError: 'NoneType' object has no attribute 'IsInitialized'

有人可以帮我解决或理解为什么会这样吗?

标签: pythonprotocol-buffersgrpcgrpc-python

解决方案


它似乎None是从 gRPC 库返回的,可能存在静默异常。如果你熟悉pdb,你能尝试找到None消息的来源吗?或者,您可以通过指定环境变量打开跟踪日志GRPC_TRACE=all GRPC_VERBOSITY=debug,请参阅文档

如果问题仍然存在,请考虑将其报告给https://github.com/grpc/grpc/issues并附上复制片段。


推荐阅读