【问题标题】:GRPC: remote error and missing parameters from messageGRPC:远程错误和消息中缺少参数
【发布时间】:2016-06-09 09:29:46
【问题描述】:

我正在使用 grpc 运行一个简单的 python 服务器应用程序。这是服务器代码:

类分类器(grpc_cl.BetaClassifierServicer):

def __init__(self):
    default_config = self.getDefaultConfig()
    self.engine_config = default_config["engine"]
    self.port = default_config["daemon"]["port"]
    # self.engine = loadLSTM3Model(self.engine_config)

def getDefaultConfig(self):
    with open("service.properties.yaml", "r") as stream:
        default_config = yaml.load(stream)
    return default_config

def Analyze(self, request, context):
    file_name = request.sentences_file
    print "This is the file to analyze ", file_name
    error = grpc_cl.Error(error_code = 0, error_message = "OK")

    return grpc_cl.CategoryReply(error)

客户:

channel = implementations.insecure_channel('localhost', 50051)  
stub = classifier_grpc.beta_create_Classifier_stub(channel)
reply = stub.Analyze(classifier_grpc.CategoryRequest(user_context=1, sentences_file="file"), 10000)
print 'Answer', reply.error.error_message

以及带有消息的 .proto 文件:

syntax = "proto3";

service Classifier{
    rpc Analyze(CategoryRequest) returns (CategoryReply){}
    rpc Train(TrainRequest) returns (CategoryReply){}
}

message CategoryRequest{
    int32 user_context = 1;
    string sentences_file = 2;
}
message CategoryReply{
    Error error = 1;
    string categories_file = 2;
}
message Error{
    int32 error_code = 1;
    string error_message = 2;
}

启动服务器和客户端,并将它们都连接到各自的端​​口,给我这个错误:

Traceback (most recent call last):
  File "/home/~/service/client.py", line 19, in <module>
    reply = stub.Analyze(classifier_grpc.CategoryRequest(user_context=1, sentences_file="file"), 10000)
  File "/usr/local/lib/python2.7/dist-packages/grpc/framework/crust/implementations.py", line 73, in __call__
    protocol_options, metadata, request)
  File "/usr/local/lib/python2.7/dist-packages/grpc/framework/crust/_calls.py", line 109, in blocking_unary_unary
    return next(rendezvous)
  File "/usr/local/lib/python2.7/dist-packages/grpc/framework/crust/_control.py", line 412, in next
    raise self._termination.abortion_error
grpc.framework.interfaces.face.face.RemoteError

现在有人知道为什么会这样吗?另外,我可以从 CategoryRequest 中提取 user_context,但不能从 sentences_file 字符串中提取,那个是空白的。

【问题讨论】:

    标签: python grpc


    【解决方案1】:

    grpc.framework.interfaces.face.face.RemoteError表示服务器在处理请求时发生了异常。

    在你的情况下,protobuf参数需要通过关键字指定,即

    return grpc_cl.CategoryReply(error)

    应该是

    return grpc_cl.CategoryReply(error=error)

    【讨论】:

    • 至于缺少的sentences_file,我相信你的print语句是不正确的,导致你得出这个结论。 ` print "This is the file to analyze {}".format(file_name)` 应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多