0

我在 python 2.7 中使用 grpc 和 protobuf 2.6.1,当我运行客户端代码时,出现以下错误:

 Traceback (most recent call last):
 File "debate_client.py", line 31, in <module>
 run_client()
 File "debate_client.py", line 17, in run_client
 reply = stub.Answer(debate_pb2.AnswerRequest(question=question, timeout=timeout), 30)
 File "/Users/elaine/Desktop/gitHub/grpc/python2.7_virtual_environment/lib/python2.7/site-packages/grpc/framework/crust/implementations.py", line 73, in __call__
 protocol_options, metadata, request)
 File "/Users/elaine/Desktop/gitHub/grpc/python2.7_virtual_environment/lib/python2.7/site-packages/grpc/framework/crust/_calls.py", line 109, in blocking_unary_unary
 return next(rendezvous)
 File "/Users/elaine/Desktop/gitHub/grpc/python2.7_virtual_environment/lib/python2.7/site-packages/grpc/framework/crust/_control.py", line 412, in next
 raise self._termination.abortion_error
 grpc.framework.interfaces.face.face.RemoteError: RemoteError(code=StatusCode.UNKNOWN, details="")

这是我的客户端代码:

from grpc.beta import implementations
import debate_pb2
import sys

def run_client():
    params = sys.argv
    print params
    how = params[1]
    question = params[2]

    channel = implementations.insecure_channel('localhost', 29999)
    stub = debate_pb2.beta_create_Candidate_stub(channel)
    if how.lower() == "answer":
        timeout = int(params[3])
        reply = stub.Answer(debate_pb2.AnswerRequest(question=question, timeout=timeout), 30)

    elif how.lower() == "elaborate":
        blah = params[3:len(sys.argv)]
        for i in range(0, len(blah)):
            blah[i] = int(blah[i])
        reply = stub.Elaborate(debate_pb2.ElaborateRequest(topic=question, blah_run=blah), 30)

    if reply is None:
        print "No comment"
    else:
        print reply.answer

if __name__ == "__main__":
    run_client()

这是我的服务器端代码:

import debate_pb2
import consultation_pb2
import re
import random
from grpc.beta import implementations


class Debate(debate_pb2.BetaCandidateServicer):

    def Answer(self, request, context=None):
        #Answer implementation
    def Elaborate(self, request, context=None):
        #Elaborate implementation

def run_server():
    server = debate_pb2.beta_create_Candidate_server(Debate())
    server.add_insecure_port('localhost:29999')
    server.start()

if __name__ == "__main__":
    run_server()

知道远程错误来自哪里吗?太感谢了!

4

3 回答 3

0

刚才我也遇到了同样的情况,我明白了原因。

确保原型定义中的消息和实现中的消息与格式匹配。

例如,我的Response消息message=在我的 python 服务器中有一个参数,但在我的原型定义中没有。

于 2016-02-21T04:01:36.637 回答
0

您好 Elaine,感谢您试用 gRPC Python。

没有什么比明显的吸烟枪更能突显我的意思,但我看到的几件事是:

  1. gRPC Python 不知道可以与 protobuf 2.6.1 一起使用。您是否尝试过使用最新的 protobuf 版本(此时为 3.0.0a3)?

  2. context不是服务方法中的可选关键字参数;这是一个必需的位置参数。=None从您的服务方法实现中删除是否会影响任何更改?

于 2015-12-04T04:58:14.943 回答
0

我认为您的功能实现应该在外部class Debate,或者您的功能可能没有正确实现以提供所需的结果。

我遇到了类似的错误,因为我的函数在类内,但将它移到类外修复了它。

于 2016-03-06T12:44:13.027 回答