def create_client(client_klass, *, host=None, port=None, loop=None, timeouts=None, client_type=None): """ create a asyncio thrift client and return a context manager for it This is a coroutine :param client_klass: thrift Client class :param host: hostname/ip, None = loopback :param port: port number :param loop: asyncio event loop :returns: a Context manager which provides the thrift client """ if not loop: loop = asyncio.get_event_loop() transport, protocol = yield from loop.create_connection( ThriftClientProtocolFactory( client_klass, loop=loop, timeouts=timeouts, client_type=client_type, ), host=host, port=port, ) return protocol_manager(protocol)
def test_server_with_client(sock, loop): port = sock.getsockname()[1] (transport, protocol) = yield from loop.create_connection( ThriftClientProtocolFactory(Calculator.Client), host='localhost', port=port) client = protocol.client sum = yield from asyncio.wait_for(client.add(1, 2), None) return sum
def create_client( client_klass, *, host=None, port=None, sock=None, loop=None, timeouts=None, client_type=None, ssl=None, ): """ create an asyncio thrift client and return an async context manager that can be used as follows: async with create_client(smc2_client, port=1421) as smc: await smc.getStatus() This can be used in the old way: with (await create_client(smc2_client, port=1421)) as smc: await smc.getStatus() or even the old deprecated way: with (yield from create_client(smc2_client, port=1421) as smc: yield from smc.getStatus() :param client_klass: thrift Client class :param host: hostname/ip, None = loopback :param port: port number :param sock: socket.socket object :param loop: asyncio event loop :returns: an Async Context Manager """ if not loop: loop = asyncio.get_event_loop() coro = loop.create_connection( ThriftClientProtocolFactory( client_klass, loop=loop, timeouts=timeouts, client_type=client_type, ), host=host, port=port, sock=sock, ssl=ssl, ) return async_protocol_manager(coro)
def test_server_with_client(sock, loop): port = sock.getsockname()[1] (transport, protocol) = yield from loop.create_connection( ThriftClientProtocolFactory(Calculator.Client, loop=loop), host='localhost', port=port, ) client = protocol.client add_result = yield from asyncio.wait_for( client.add(1, 2), timeout=None, loop=loop, ) transport.close() protocol.close() return add_result
def setUp(self): global loop self.host = '127.0.0.1' self.handler = TestHandler() self.server = yield from ThriftAsyncServerFactory( self.handler, interface=self.host, port=0, loop=loop, ) self.port = self.server.sockets[0].getsockname()[1] self.transport, self.protocol = yield from loop.create_connection( ThriftClientProtocolFactory(ThriftTest.Client), host=self.host, port=self.port, ) self.client = self.protocol.client
async def setUp(self): global loop self.host = '127.0.0.1' self.handler = TestHandler(use_async=True) self.server = await ThriftAsyncServerFactory( self.handler, interface=self.host, port=0, loop=loop, ) self.port = self.server.sockets[0].getsockname()[1] self.transport, self.protocol = await loop.create_connection( ThriftClientProtocolFactory(ThriftTest.Client, client_type=self.CLIENT_TYPE), host=self.host, port=self.port, ) self.client = self.protocol.client
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()
async def open(self): host, port = await self._lookup_service() timeouts = await self._get_timeouts() conn_fut = self.loop.create_connection( ThriftClientProtocolFactory(self._client_class, timeouts=timeouts), host=host, port=port, ) (transport, protocol) = await asyncio.wait_for(conn_fut, self._open_timeout, loop=self.loop) self._inc_counter("connected") self._protocol = protocol self._transport = transport self._client = protocol.client # hookup the close method to the client self._client.close = self.close self._connected = True return self._client
sys.path.append( os.path.join(os.path.dirname(os.path.abspath(__file__)), "gen-py")) from thrift.server.TAsyncioServer import ThriftClientProtocolFactory from mytest import TestService if __name__ == "__main__": loop = asyncio.get_event_loop() certfile = "/root/ssl-stuff/client.pem" context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.verify_mode = ssl.CERT_REQUIRED context.load_cert_chain(certfile=certfile, keyfile=certfile) context.load_verify_locations(cafile="/root/ssl-stuff/rootca.pem") connection = loop.create_connection( ThriftClientProtocolFactory(TestService.Client, loop=loop), host="localhost", port=29292, ssl=context, ) _, protocol = loop.run_until_complete(connection) result = loop.run_until_complete(protocol.client.add(5, 2)) print(f"Add: {result}") loop.close() protocol.close() sys.exit(0)