小编fed*_*ist的帖子

是否可以在单个 GRPC 服务器上运行同一服务的多个实例?

我试图看看是否可以在单个 GRPC 服务器上运行同一服务的不同实例,但看起来我无法这样做。所以我想知道我的测试是否做错了什么,或者根本不可能。

我的测试基于grpc 存储库中的示例/python/多路复用:

服务:

class _GreeterServicer(helloworld_pb2_grpc.GreeterServicer):

    def __init__(self, greeter):
        self.greeter = greeter

    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(
            message='Hello, {}! This is {}!'.format(request.name, self.greeter))
Run Code Online (Sandbox Code Playgroud)

服务器:

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("John"),
                                                      server)
    helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("Jim"),
                                                      server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()
Run Code Online (Sandbox Code Playgroud)

客户:

def run():
    for _ in range(10):
        with grpc.insecure_channel('localhost:50051') as channel:
            greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
            greeter_response = greeter_stub.SayHello(
                helloworld_pb2.HelloRequest(name='you'))
            print("Greeter client received: " + greeter_response.message)
Run Code Online (Sandbox Code Playgroud)

由于我为每次迭代打开一个新通道,因此我期望得到包含“你好,你!这是吉姆!”的输出。和“你好!我是约翰!”,但我只得到:

Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is …
Run Code Online (Sandbox Code Playgroud)

python grpc protobuf-python

5
推荐指数
1
解决办法
2780
查看次数

标签 统计

grpc ×1

protobuf-python ×1

python ×1