【问题标题】:Method not implemented!, "grpc_status":12方法未实现!,“grpc_status”:12
【发布时间】:2021-09-02 08:07:31
【问题描述】:

我创建了一个 GRPC 服务和客户端。服务正在运行并托管在端口 5001 上,但客户端抛出 GRPC 错误,因为方法未实现。

服务器

class CalculatorService(Calculator_pb2_grpc.CalculatorServicer):

    def SquareRoot(self, request, context):
        response = Calculator_pb2.Number()
        response.value = Calculator.square_root(request.value)
        return response


server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

Calculator_pb2_grpc.add_CalculatorServicer_to_server(
    Calculator_pb2_grpc.CalculatorServicer(),
    server)

print('Starting Server on port 5001')

server.add_insecure_port('[::]:5001')
server.start()

print('Server Started')

server.wait_for_termination()

客户

channel = grpc.insecure_channel('localhost:5001')

number = Calculator_pb2.Number(value=16)

response = Calculator_pb2_grpc.CalculatorStub(channel).SquareRoot(number)

print(response.value)

错误

C:\Users\anshul_jain\.virtualenvs\pythonCalc-hTzam63Y\Scripts\python.exe C:/Projects/GRPC/pythonCalc/client.py
Traceback (most recent call last):
  File "C:/Projects/GRPC/pythonCalc/client.py", line 8, in <module>
    response = Calculator_pb2_grpc.CalculatorStub(channel).SquareRoot(number)
  File "C:\Users\anshul_jain\.virtualenvs\pythonCalc-hTzam63Y\lib\site-packages\grpc\_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "C:\Users\anshul_jain\.virtualenvs\pythonCalc-hTzam63Y\lib\site-packages\grpc\_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)

grpc._channel._InactiveRpcError: _InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNIMPLEMENTED
    details = "Method not implemented!"
    debug_error_string = "{"created":"@1630568886.349000000","description":"Error received from 
 peer \u00109\u00cc\u008f\u00a8\u0001",
    "file":"src/core/lib/surface/call.cc",
"file_line":1070,"grpc_message":"Method not implemented!","grpc_status":12}"
Process finished with exit code 1

【问题讨论】:

    标签: python grpc


    【解决方案1】:

    请包含您用于生成客户端|服务器存根的 proto 文件。

    从您的代码中不清楚,但SquareRoot 必须作为方法出现在服务上并由服务器实现。你展示了后者,但没有展示前者。

    可能值得考虑使用诸如gRPCurl 之类的工具来测试您的服务。

    另外,SquareRoot 的实现似乎不正确。您将需要使用 math.sqrt() 之类的东西来计算要返回的平方根(并检查异常情况)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多