def setUp(self): self._socket, self._target = _get_free_loopback_tcp_port() self._server = _Server(self._target) self._server.start() self._loop = asyncio.new_event_loop() asyncio.set_event_loop(self._loop) aio.init_grpc_aio()
async def on_startup(): aiogrpc.init_grpc_aio() self.aiogrpc_gcs_channel = await ( make_gcs_grpc_channel(redis_client)) self.gcs_heartbeat_info_stub = ( gcs_service_pb2_grpc.HeartbeatInfoGcsServiceStub( self.aiogrpc_gcs_channel))
def test_multi_ephemeral_loops(self): # Initializes AIO module outside. It's part of the test. We especially # want to ensure the closing of the default loop won't cause deadlocks. aio.init_grpc_aio() async def ping_pong(): address, server = await start_test_server() channel = aio.insecure_channel(address) stub = test_pb2_grpc.TestServiceStub(channel) await stub.UnaryCall(messages_pb2.SimpleRequest()) await channel.close() await server.stop(None) for i in range(_NUM_OF_LOOPS): old_loop = asyncio.get_event_loop() old_loop.close() loop = asyncio.new_event_loop() loop.set_debug(True) asyncio.set_event_loop(loop) loop.run_until_complete(ping_pong()) aio.shutdown_grpc_aio()
async def test_interoperability(): aio.init_grpc_aio() args = interop_client_lib.parse_interop_client_args() channel = _create_channel(args) stub = interop_client_lib.create_stub(channel, args) test_case = _test_case_from_arg(args.test_case) await methods.test_interoperability(test_case, stub, args)
def __init__(self) -> None: init_grpc_aio() self.server = grpc.experimental.aio.server() self.servicer = HelloServicer() add_HelloWorldServiceServicer_to_server(self.servicer, self.server) self.server.add_insecure_port("[::]:50051")
async def run_sync_request(loop): global finish_benchmark aio.init_grpc_aio() channel = grpc.insecure_channel("127.0.0.1:50051") stub = echo_pb2_grpc.EchoStub(channel) for i in range(100): response = stub.Hi(echo_pb2.EchoRequest(message="ping")) print("eureka")
async def benchmark(loop, seconds=DEFAULT_SECONDS, concurrency=DEFAULT_CONCURRENCY): global finish_benchmark aio.init_grpc_aio() print("Creating channels and warmming up ....") multicallables = [] for i in range(concurrency): channel = aio.insecure_channel("127.0.0.1:50051") multicallable = channel.unary_unary( '/echo.Echo/Hi', request_serializer=echo_pb2.EchoRequest.SerializeToString, response_deserializer=echo_pb2.EchoReply.FromString) response = await multicallable(echo_pb2.EchoRequest(message="ping")) assert response multicallables.append(multicallable) print("Starting tasks ....") tasks = [ asyncio.ensure_future(requests(idx, multicallable)) for idx, multicallable in enumerate(multicallables) ] await asyncio.sleep(seconds) print("Finishing tasks ....") finish_benchmark = True while not all([task.done() for task in tasks]): await asyncio.sleep(0) times = [] for task in tasks: times += task.result() times.sort() total_requests = len(times) avg = sum(times) / total_requests p75 = times[int((75 * total_requests) / 100)] p90 = times[int((90 * total_requests) / 100)] p99 = times[int((99 * total_requests) / 100)] print('QPS: {0}'.format(int(total_requests / seconds))) print('Avg: {0:.6f}'.format(avg)) print('P75: {0:.6f}'.format(p75)) print('P90: {0:.6f}'.format(p90)) print('P99: {0:.6f}'.format(p99))
def test_behavior_outside_asyncio(self): # Ensures non-AsyncIO object can be initiated channel_creds = grpc.ssl_channel_credentials() # Ensures AsyncIO API not raising outside of AsyncIO. # NOTE(lidiz) This behavior is bound with GAPIC generator, and required # by test frameworks like pytest. In test frameworks, objects shared # across cases need to be created outside of AsyncIO coroutines. aio.insecure_channel('') aio.secure_channel('', channel_creds) aio.server() aio.init_grpc_aio() aio.shutdown_grpc_aio()
async def run_worker_server(port: int) -> None: aio.init_grpc_aio() server = aio.server() servicer = worker_servicer.WorkerServicer() worker_service_pb2_grpc.add_WorkerServiceServicer_to_server( servicer, server) server.add_insecure_port('[::]:{}'.format(port)) await server.start() await servicer.wait_for_quit() await server.stop(None)
def run(self): self._add_eggs_to_path() from grpc.experimental.aio import init_grpc_aio init_grpc_aio() import tests loader = tests.Loader() loader.loadTestsFromNames(['tests_aio']) # Even without dedicated threads, the framework will somehow spawn a # new thread for tests to run upon. New thread doesn't have event loop # attached by default, so initialization is needed. runner = tests.Runner(dedicated_threads=False) result = runner.run(loader.suite) if not result.wasSuccessful(): sys.exit('Test failure')
def test_behavior_outside_asyncio(self): # Ensures non-AsyncIO object can be initiated channel_creds = grpc.ssl_channel_credentials() # Ensures AsyncIO API NOT working outside of AsyncIO with self.assertRaises(RuntimeError): aio.insecure_channel('') with self.assertRaises(RuntimeError): aio.secure_channel('', channel_creds) with self.assertRaises(RuntimeError): aio.server() # Ensures init_grpc_aio fail outside of AsyncIO with self.assertRaises(RuntimeError): aio.init_grpc_aio()
async def serve(): init_grpc_aio() args = interop_server_lib.parse_interop_server_arguments() if args.use_tls: credentials = interop_server_lib.get_server_credentials() address, server = await _test_server.start_test_server( port=args.port, secure=True, server_credentials=credentials) else: address, server = await _test_server.start_test_server( port=args.port, secure=False, ) _LOGGER.info('Server serving at %s', address) await server.wait_for_termination() _LOGGER.info('Server stopped; exiting.')
async def start(self) -> None: """ :return: :rtype: None """ init_grpc_aio() self.server = server() # TODO: add the gRPC servicer to server address = self.config["ADDRESS"] # secure mode if private_keys := self.config.get("SERVER_CREDENTIALS_PRIVATE_KEYS"): with open(private_keys, "rb") as file: keys: bytes = file.read() certs: Optional[bytes] if certificates := self.config.get( "SERVER_CREDENTIALS_CERTIFICATES"): with open(certificates, "rb") as file: certs = file.read()
from aiohttp import hdrs from grpc.experimental import aio as aiogrpc import ray._private.services import ray.new_dashboard.consts as dashboard_consts import ray.new_dashboard.utils as dashboard_utils from ray import ray_constants from ray.core.generated import gcs_service_pb2 from ray.core.generated import gcs_service_pb2_grpc from ray.new_dashboard.datacenter import DataOrganizer from ray.new_dashboard.utils import async_loop_forever logger = logging.getLogger(__name__) routes = dashboard_utils.ClassMethodRouteTable aiogrpc.init_grpc_aio() class DashboardHead: def __init__(self, http_host, http_port, http_port_retries, redis_address, redis_password, log_dir): # HeartbeatInfoGcsService self._gcs_heartbeat_info_stub = None self._gcs_check_alive_seq = 0 self._gcs_rpc_error_counter = 0 # Public attributes are accessible for all head modules. # Walkaround for issue: https://github.com/ray-project/ray/issues/7084 self.http_host = "127.0.0.1" if http_host == "localhost" else http_host self.http_port = http_port self.http_port_retries = http_port_retries self.redis_address = dashboard_utils.address_tuple(redis_address)
def _get_default_loop(debug=True): try: loop = asyncio.get_event_loop() except: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) finally: loop.set_debug(debug) return loop # NOTE(gnossen) this test class can also be implemented with metaclass. class AioTestBase(unittest.TestCase): @property def loop(self): return _get_default_loop() def __getattribute__(self, name): """Overrides the loading logic to support coroutine functions.""" attr = super().__getattribute__(name) # If possible, converts the coroutine into a sync function. if name.startswith('test_') or name in _COROUTINE_FUNCTION_ALLOWLIST: if asyncio.iscoroutinefunction(attr): return _async_to_sync_decorator(attr, _get_default_loop()) # For other attributes, let them pass. return attr aio.init_grpc_aio()
def main(): aio.init_grpc_aio() loop = asyncio.get_event_loop() loop.create_task(_start_async_server()) loop.run_forever()
def setUp(self): self._loop = asyncio.new_event_loop() asyncio.set_event_loop(self._loop) aio.init_grpc_aio()
bucket = await s3resource.Bucket(request.bucket) async for obj in bucket.objects.all(): e_tag = await obj.e_tag print(obj.key, e_tag) total_count += 1 print(total_count) await context.write(AwsAPI_pb2.ObjectReply(count=total_count)) async def serve(): server = grpc.server() AwsAPI_pb2_grpc.add_S3Servicer_to_server(S3(), server) server.add_insecure_port('[::]:50051') await server.start() print('run server port 50051') await server.wait_for_termination() # def get_args(): # parser = argparse.ArgumentParser() # parser.add_argument('--workers', '-w', type=int, required=True, dest='workers') # # return parser.parse_args() if __name__ == '__main__': logging.basicConfig() # args = get_args() grpc.init_grpc_aio() asyncio.run(serve())