GRPC:远程错误和消息中缺少的参数

时间:2016-06-09 09:29:46

标签: python grpc

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

class Classifier(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,但不能从sentence_file字符串中提取一个空白。

1 个答案:

答案 0 :(得分:1)

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

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

return grpc_cl.CategoryReply(error)

应该是

return grpc_cl.CategoryReply(error=error)