Newer
Older
grpcdemo / python / grpcdemo_client_async.py
# Example of async call to gRPC server


import gc
import grpc
import grpcdemo_pb2
import grpcdemo_pb2_grpc

import sys
import time
import tornado.ioloop

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('%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().cmd_results


# Loop over command line args calling remove server
# then wait for answers

if __name__ == '__main__':

  for cmd in ARGS:
    grpc_call(cmd, MyCallback)
    print "Called with %s ..." % cmd
    gc.collect()             # Just to prove rendevous object is still there

print "Waiting For Response(s)...\n\n"


# Nonblocking loop

tornado.ioloop.IOLoop.instance().start()