Protocol buffers grpc错误…找不到记录器的处理程序;grpc.“通用”;

Protocol buffers grpc错误…找不到记录器的处理程序;grpc.“通用”;,protocol-buffers,grpc,grpc-python,Protocol Buffers,Grpc,Grpc Python,我刚刚了解了grpc,并尝试构建一个定制的客户机-服务器过程,其中客户机发送一个id和一个与该id对应的名称 以下是custom.proto文件: syntax = "proto3" ; // Interface exported by the server service Detail { rpc GetName(idx) returns (namex) {} } message idx { int32 id = 1; } message namex{ int32 i

我刚刚了解了grpc,并尝试构建一个定制的客户机-服务器过程,其中客户机发送一个id和一个与该id对应的名称

以下是custom.proto文件:

 syntax = "proto3" ;

// Interface exported by the server
service Detail {
   rpc GetName(idx) returns (namex) {}
}

message idx {
   int32 id = 1;
}

message namex{
   int32 id = 1;
   string name = 2;
}
从该原型文件生成custom_pb2和custom_pb2_grpc.py

这是自定义的_db.json

[{"id": 0, "name":"kiran"},
 {"id":1, "name":"kirthana"},
 {"id":2, "name":"kishore"}
]
这是custom_resources.py

import json

import custom_pb2


def read_custom_database():
    ''' Reads the custom database I created.'''
    names_list = []
    with open("custom_db.json") as custom_db_file:
        for item in json.load(custom_db_file):
            itemx = custom_pb2.namex(id=item["id"], name=item["name"])
            names_list.append(itemx)
    return names_list
这是自定义的_server.py

import custom_pb2_grpc
import custom_resources
import time

_ONE_DAT_IN_SECONDS = 60*60*24

def get_name(custom_db,idx):
    '''Returns name of a given id or none'''
    for namex in custom_db:
        if namex.id == idx:
            return namex.name
    return None


class DetailServicer(custom_pb2_grpc.DetailServicer):
    """Provides methods that implements the custom server."""

    def __init__(self):
        self.db = custom_resources.read_custom_database()

    def GetName(self, request, context):
        name = get_name(self.db, request)
        if name is None:
            return "Not Found"
        else:
            return name


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    custom_pb2_grpc.add_DetailServicer_to_server(DetailServicer(), server)
    server.add_insecure_port('[::]:12345')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAT_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    serve()
这是custom_client.py

from __future__ import print_function

import random

import grpc

import custom_pb2
import custom_pb2_grpc
import custom_resources


def custom_get_one_name(stub, idx):
    name = stub.GetName(idx)
    print("Feater called with id %d returned: %s" %(idx,name))
    return

def custom_get_names(stub):
    custom_get_one_name(stub,2)
    custom_get_one_name(stub,1)


def run():
    with grpc.insecure_channel('localhost:12345') as channel:
        stub = custom_pb2_grpc.DetailStub(channel)
        custom_get_names(stub)

if __name__ == '__main__':
    run()
我得到的确切错误消息是:

No handlers could be found for logger "grpc._common"
Traceback (most recent call last):
  File "custom_client.py", line 30, in <module>
    run()
  File "custom_client.py", line 27, in run
    custom_get_names(stub)
  File "custom_client.py", line 19, in custom_get_names
    custom_get_one_name(stub,2)
  File "custom_client.py", line 13, in custom_get_one_name
    name = stub.GetName(idx)
  File "/usr/local/lib/python2.7/dist-packages/grpc/_channel.py", line 513, in __call__
    state, call, = self._blocking(request, timeout, metadata, credentials)
  File "/usr/local/lib/python2.7/dist-packages/grpc/_channel.py", line 500, in _blocking
    raise rendezvous
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
    status = StatusCode.INTERNAL
    details = "Exception serializing request!"
    debug_error_string = "None"
找不到记录器“grpc.\u common”的处理程序
回溯(最近一次呼叫最后一次):
文件“custom_client.py”,第30行,在
运行()
文件“custom_client.py”,第27行,正在运行
自定义\u获取\u名称(存根)
文件“custom_client.py”,第19行,在custom_get_名称中
自定义\u获取\u一个\u名称(存根,2)
文件“custom_client.py”,第13行,在custom_get_one_name中
name=stub.GetName(idx)
文件“/usr/local/lib/python2.7/dist packages/grpc/_channel.py”,第513行,在调用中__
状态、调用、=self.\u阻塞(请求、超时、元数据、凭据)
文件“/usr/local/lib/python2.7/dist packages/grpc/_channel.py”,第500行,in_blocking
升起集合点

grpc.\u channel.\u Rendezvous:您试图传递一个整数作为请求
自定义\u获取一个\u名称(存根,2)
,其中需要一个类型为
idx
的协议缓冲消息。您应该创建一条
idx
消息,并像以下那样传递该消息:

custom_get_one_name(stub, custom_pb2_grpc.idx(id=2))