def main(client): yield client.ping() print('ping()') sum = yield client.add(1, 1) print(('1+1=%d' % (sum))) work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = yield client.calculate(1, work) print('Whoa? You know how to divide by zero?') print('FYI the answer is %d' % quotient) except InvalidOperation as e: print(('InvalidOperation: %r' % e)) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = yield client.calculate(1, work) print(('15-10=%d' % (diff))) log = yield client.getStruct(1) print(('Check log: %s' % (log.value))) reactor.stop()
def communicate(): # create client transport = TTornado.TTornadoStreamTransport('localhost', 9090) # open the transport, bail on error try: yield transport.open() print('Transport is opened') except TTransport.TTransportException as ex: logging.error(ex) raise gen.Return() pfactory = TBinaryProtocol.TBinaryProtocolFactory() client = Calculator.Client(transport, pfactory) # ping yield client.ping() print("ping()") # add sum_ = yield client.add(1, 1) print("1 + 1 = {0}".format(sum_)) # make a oneway call without a callback (schedule the write and continue # without blocking) client.zip() print("zip() without callback") # make a oneway call with a callback (we'll wait for the stream write to # complete before continuing) client.zip() print("zip() with callback") # calculate 1/0 work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = yield client.calculate(1, work) print("Whoa? You know how to divide by zero ? -> {0}".format(quotient)) except InvalidOperation as io: print("InvalidOperation: {0}".format(io)) # calculate 15-10 work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = yield client.calculate(1, work) print("15 - 10 = {0}".format(diff)) # getStruct log = yield client.getStruct(1) print("Check log: {0}".format(log.value)) # close the transport client._transport.close() raise gen.Return()
def main(): root = logging.getLogger() root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.NOTSET) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]') ch.setFormatter(formatter) root.addHandler(ch) parser = argparse.ArgumentParser() parser.add_argument("--mode") args = parser.parse_args() assert args.mode in ["cluster", "local"], "mode can only be `local` or `cluster`" start_with_rpc = (args.mode == "cluster") if start_with_rpc: logging.info("Starting with RPC!") else: logging.info("Starting local mode, expecting Singleton!") client = CalculatorHandler(use_rpc=start_with_rpc, server=False) client.ping() logging.info('ping()') sum_ = client.add(1, 1) logging.info('1+1=' + str(sum_)) work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = client.calculate(1, work) logging.info('Whoa? You know how to divide by zero?') logging.info('FYI the answer is: ' + str(quotient)) except InvalidOperation as e: logging.info('InvalidOperation: %r' % e) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) logging.info('15-10=' + str(diff)) log = client.get_struct(1) logging.info('Check log: ' + str(log.value)) client.done()
def main(): # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Calculator.Client(protocol) # Connect! transport.open() client.ping() print('ping()') sum_ = client.add(1, 1) print('1+1=%d' % sum_) work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = client.calculate(1, work) print('Whoa? You know how to divide by zero?') print('FYI the answer is %d' % quotient) except InvalidOperation as e: print('InvalidOperation: %r' % e) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) print('15-10=%d' % diff) log = client.getStruct(1) print('Check log: %s' % log.value) result = client.request_matching_symbols(request_json="AA") print("IB Symbols query result AA = " + str(result)) # Close! transport.close()
def main(): # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Calculator.Client(protocol) # Connect! transport.open() client.ping() print('ping()') sum = client.add(1, 1) print(('1+1=%d' % (sum))) work = Work() work.op = Operation.DIVIDE work.num1 = 2 work.num2 = 3 try: quotient = client.calculate(1, work) print('Whoa? You know how to divide by zero?') print('FYI the answer is %d' % quotient) except InvalidOperation as e: print(('InvalidOperation: %r' % e)) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) print(('15-10=%d' % (diff))) log = client.getStruct(1) print(('Check log: %s' % (log.value))) # Close! transport.close()
def main(): # Make socket transport = TLambdaClientTransport("tutorial-server") # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Calculator.Client(protocol) # Connect! transport.open() print('going to ping') client.ping() print('ping()') sum_ = client.add(1, 1) print('1+1=%d' % sum_) work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = client.calculate(1, work) print('Whoa? You know how to divide by zero?') print('FYI the answer is %d' % quotient) except InvalidOperation as e: print('InvalidOperation: %r' % e) work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) print('15-10=%d' % diff) log = client.getStruct(1) print('Check log: %s' % log.value) # Close! transport.close()
def communicate(callback=None): # create client transport = TTornado.TTornadoStreamTransport("localhost", 9090) pfactory = TBinaryProtocol.TBinaryProtocolFactory() client = Calculator.Client(transport, pfactory) # open the transport, bail on error try: yield gen.Task(transport.open) except TTransport.TTransportException as ex: logging.error(ex) if callback: callback() return # ping yield gen.Task(client.ping) print "ping()" # add sum_ = yield gen.Task(client.add, 1, 1) print "1 + 1 = {}".format(sum_) # make a oneway call without a callback (schedule the write and continue # without blocking) client.zip() print "zip() without callback" # make a oneway call with a callback (we'll wait for the stream write to # complete before continuing) yield gen.Task(client.zip) print "zip() with callback" # calculate 1/0 work = Work() work.op = Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: quotient = yield gen.Task(client.calculate, 1, work) print "Whoa? You know how to divide by zero?" except InvalidOperation as io: print "InvalidOperation: {}".format(io) # calculate 15-10 work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = yield gen.Task(client.calculate, 1, work) print "15 - 10 = {}".format(diff) # getStruct log = yield gen.Task(client.getStruct, 1) print "Check log: {}".format(log.value) # close the transport client._transport.close() if callback: callback()