0

1、proto

service RouteGuide { 
  rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}
message RouteNote {
  // The location from which the message is sent.
  Point location = 1;

  // The message to be sent.
  string message = 2;
}

2、local client

def generate_messages():
    messages = [
        make_route_note("First message", 0, 0),
        make_route_note("Second message", 0, 1),
        make_route_note("Third message", 1, 0),
        make_route_note("Fourth message", 0, 0),
        make_route_note("Fifth message", 1, 0),
    ]
    for msg in messages:
        print("Sending %s at %s" % (msg.message, msg.location))
        yield msg


def guide_route_chat(stub):
    responses = stub.RouteChat(generate_messages())
    for response in responses:
        print("Received message %s at %s" % (response.message, response.location))

def run():
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = route_guide_pb2_grpc.RouteGuideStub(channel)
        print("-------------- RouteChat --------------")
        guide_route_chat(stub)

if __name__ == '__main__':
    run()

3、Server

class RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer):
    def RouteChat(self, request_iterator, context):
        yield from request_iterator

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    route_guide_pb2_grpc.add_RouteGuideServicer_to_server(RouteGuideServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    serve()

4、my question

I want to spwan two clients in RouteGuideServicer.RouteChat method. The two clients is stub.RouteChat same with the local client above. like this:

class RouteGuideServicer(route_guide_pb2_grpc.RouteGuideServicer):
    def RouteChat(self, request_iterator, context):
        with grpc.insecure_channel('node1:50051') as channel:
            stub = route_guide_pb2_grpc.RouteGuideStub(channel)
            yield from stub.RouteChat(request_iterator)
        with grpc.insecure_channel('node2:50051') as channel:
            stub = route_guide_pb2_grpc.RouteGuideStub(channel)
            yield from stub.RouteChat(request_iterator)

but i want node1 and node2 receive same data, response from node1 and node2 return to local client promptly.

enter image description here

help me! thanks a lot.

sorry for my poor English

iskylite
  • 1
  • 1
  • > but i want node1 and node2 receive same data, response from node1 and node2 return to local client promptly. It's not clear to me what you mean by this. Can you please elaborate? – Richard Belleville Jul 20 '22 at 17:49
  • @Richard Belleville one local client will connect to a remote serevr to send data stream, the server will spawn same clients connecting other remote serevrs to transport same data stream (the server after receiving all data stream will send response to client, and if a client is spawned by the server, the server will send client response to the client connecting the server ) . the first local client can receive all server response promptly. – iskylite Jul 21 '22 at 01:42
  • I'm still not sure exactly what you are asking. You should be able to have your server connected to other servers as a client, and forward incoming requests to them. Generally you don't want to create those clients on each RPC, but create them when the server starts up and re-use them for every incoming RPC. Are you having trouble with that approach? – Doug Fawley Jul 27 '22 at 18:39

0 Answers0