diff --git a/proto/grpcdemo.proto b/proto/grpcdemo.proto index 9a1eb41..47a4f8b 100644 --- a/proto/grpcdemo.proto +++ b/proto/grpcdemo.proto @@ -11,15 +11,15 @@ rpc runcommand (Command) returns (Results) {} } -// Argument placeholder +// The command to run on the server message Command{ string command = 1; } -// The response message containing the current date and time +// The response message containing command results message Results { - string results = 1; + string cmd_results = 1; } diff --git a/python/grpcdemo_client.py b/python/grpcdemo_client.py index f6bc196..cf12245 100644 --- a/python/grpcdemo_client.py +++ b/python/grpcdemo_client.py @@ -1,3 +1,6 @@ +# Example of async call to gRPC server + + import gc import grpc import grpcdemo_pb2 @@ -7,23 +10,44 @@ import time import tornado.ioloop -PORTNO = 6000 CALL_TIMEOUT = 30 WAIT_TIME = 10 + +HOST = sys.argv[1] +PORTNO = 6000 + +ARGS = sys.argv[2:] + + +# Note that in the call logic below, it is the 'future' method that +# makes this async. + def grpc_call(cmd, callback): - channel = grpc.insecure_channel('localhost:%s' % PORTNO) + channel = grpc.insecure_channel('%s:%s' % (HOST, PORTNO)) stub = grpcdemo_pb2_grpc.HostCommandStub(channel) stub.runcommand.future(grpcdemo_pb2.Command(command=cmd), CALL_TIMEOUT).add_done_callback(callback) + +# Callback handler - just prints returned string + def MyCallback(response): - print response.result() + print response.result().cmd_results + + +# Loop over command line args calling remove server +# then wait for answers if __name__ == '__main__': - for cmd in ("uname", "uptime", "ls"): + for cmd in ARGS: grpc_call(cmd, MyCallback) print "Called with %s ..." % cmd - gc.collect() # Prove rendevous object is still there + gc.collect() # Just to prove rendevous object is still there + +print "Waiting For Response(s)...\n\n" + + +# Nonblocking loop tornado.ioloop.IOLoop.instance().start() diff --git a/python/grpcdemo_pb2.py b/python/grpcdemo_pb2.py index f5ddce6..fd9be62 100644 --- a/python/grpcdemo_pb2.py +++ b/python/grpcdemo_pb2.py @@ -19,7 +19,7 @@ name='grpcdemo.proto', package='', syntax='proto3', - serialized_pb=_b('\n\x0egrpcdemo.proto\"\x1a\n\x07\x43ommand\x12\x0f\n\x07\x63ommand\x18\x01 \x01(\t\"\x1a\n\x07Results\x12\x0f\n\x07results\x18\x01 \x01(\t21\n\x0bHostCommand\x12\"\n\nruncommand\x12\x08.Command\x1a\x08.Results\"\x00\x62\x06proto3') + serialized_pb=_b('\n\x0egrpcdemo.proto\"\x1a\n\x07\x43ommand\x12\x0f\n\x07\x63ommand\x18\x01 \x01(\t\"\x1e\n\x07Results\x12\x13\n\x0b\x63md_results\x18\x01 \x01(\t21\n\x0bHostCommand\x12\"\n\nruncommand\x12\x08.Command\x1a\x08.Results\"\x00\x62\x06proto3') ) _sym_db.RegisterFileDescriptor(DESCRIPTOR) @@ -65,7 +65,7 @@ containing_type=None, fields=[ _descriptor.FieldDescriptor( - name='results', full_name='Results.results', index=0, + name='cmd_results', full_name='Results.cmd_results', index=0, number=1, type=9, cpp_type=9, label=1, has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, @@ -84,7 +84,7 @@ oneofs=[ ], serialized_start=46, - serialized_end=72, + serialized_end=76, ) DESCRIPTOR.message_types_by_name['Command'] = _COMMAND diff --git a/python/grpcdemo_server.py b/python/grpcdemo_server.py index 81e5dcf..76abfb1 100644 --- a/python/grpcdemo_server.py +++ b/python/grpcdemo_server.py @@ -17,7 +17,7 @@ def runcommand(self, request, context): time.sleep(DELAY) - return grpcdemo_pb2.Results(results=subprocess.check_output(request.command.split())) + return grpcdemo_pb2.Results(cmd_results=subprocess.check_output(request.command.split())) def serve():