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.
help me! thanks a lot.
sorry for my poor English