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(loop): (transport, protocol) = yield from loop.create_connection( ThriftClientProtocolFactory(Calculator.Client), host="127.0.0.1", port=8848) client = protocol.client # Wait for the server to solve this super hard problem indefinitely. sum = yield from asyncio.wait_for(client.add(1, 2), None) print("1 + 2 = {}".format(sum)) # Try divide by zero. try: work = Work(num1=2, num2=0, op=Operation.DIVIDE) yield from asyncio.wait_for(client.calculate(1, work), None) except InvalidOperation as e: print("InvalidOperation: {}".format(e)) # Make a few asynchronous calls concurrently and wait for all of them. start = time.time() calls = [ client.add(2, 3), client.add(1990, 1991), client.calculate(2, Work(num1=4, num2=2, op=Operation.MULTIPLY)), client.calculate(3, Work(num1=9, num2=3, op=Operation.SUBTRACT)), client.calculate(4, Work(num1=6, num2=8, op=Operation.ADD)), client.ping(), client.zip(), ] done, pending = yield from asyncio.wait(calls) if len(done) != len(calls): raise RuntimeError("Not all calls finished!") time_spent = time.time() - start print( "Time spent on processing {} requests: {:f} secs, results are:".format( len(calls), time_spent)) for fut in done: print(fut.result()) transport.close() protocol.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 = 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 = 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()