async def main(): conn = Connection(await asyncio.open_connection("localhost", port)) conn.start_service() x = await conn.call("increment", 10) assert x is None x = await conn.call("read") assert x is 10 x = await conn.call("increment", 6) assert x is None x = await conn.call("read") assert x is 16 with pytest.raises(RemoteException) as einfo: await conn.call("xxx") assert "xxx" in str(einfo.value) assert "not exist" in str(einfo.value) with pytest.raises(RemoteException) as einfo: await conn.call("not_public") assert "not_public" in str(einfo.value) assert "not exist" in str(einfo.value) with pytest.raises(RemoteException) as einfo: await conn.call("invalid") assert "MyException" in str(einfo.value) await conn.close()
async def main(): conn = Connection(await asyncio.open_connection("localhost", port=8500)) # Setup infrastructure conn.start_service() # Call remote service result = await conn.call("sum", 3, 2) print("Result =", result)
async def main(): conn = Connection(await asyncio.open_connection("localhost", port=8500)) # Setup infrastructure conn.start_service(ClientService()) # Call remote service result = await conn.call("sum", 3, 2) print("Result =", result) # For simplicity, we sleep for some time, to give a chace a server call us await asyncio.sleep(1)
async def client_process(): COUNT = 20000 conn = Connection(await asyncio.open_connection("localhost", port=8500)) asyncio.ensure_future(conn.serve()) conn.set_nr_error_handle(lambda x, y: 0) start_time = time.time() for _ in range(COUNT): await conn.call("sum", 1, 2) end_time = time.time() duration = end_time - start_time print("call & wait: Calls/s: {}".format(COUNT / duration)) start_time = time.time() for _ in range(COUNT): await conn.call_no_response("sum", 1, 2) end_time = time.time() duration = end_time - start_time print("call_no_response: Calls/s: {}".format(COUNT / duration))
async def _connect(self, hostname, port): key = (hostname, port) conn_f = self.connections.get(key) if conn_f is None: f = asyncio.Future() self.connections[key] = f connection = Connection(await asyncio.open_connection(hostname, port)) asyncio.ensure_future(self._serve(connection, hostname, port)) f.set_result(connection) return connection else: return await conn_f
async def main(): conn = Connection(await asyncio.open_connection("localhost", port)) conn.set_nr_error_handle(error_callback) conn.start_service() x = await conn.call_no_response("increment", 10) assert x is None x = await conn.call_no_response("increment", 10) assert x is None x = await conn.call_no_response("read") assert x is None assert error_msg[0] is None x = await conn.call_no_response("xxx", 10) x = await conn.call("read") assert error_msg[0] assert x == 20
async def main(): conn = Connection(await asyncio.open_connection("localhost", port)) asyncio.ensure_future(conn.serve(ClientService())) x = await conn.call("server_method") assert x == "data" await oneshot