def create_channel(config: "Config") -> "Channel": """ Create a :class:`Channel` for the specified configuration. """ u = urlparse(config.url.url) options = [ ("grpc.max_send_message_length", -1), ("grpc.max_receive_message_length", -1), ] if not config.url.use_http_proxy: options.append(("grpc.enable_http_proxy", 0)) if (u.scheme in ("https", "grpcs")) or config.ssl: credentials = ssl_channel_credentials( root_certificates=config.ssl.ca, private_key=config.ssl.cert_key, certificate_chain=config.ssl.cert, ) if config.access.token: credentials = composite_channel_credentials( credentials, metadata_call_credentials(GrpcAuth(config)) ) return secure_channel(u.netloc, credentials, options) else: return insecure_channel(u.netloc, options)
def create_channel(config: "Config") -> "Channel": """ Create a :class:`Channel` for the specified configuration. """ u = urlparse(config.url.url) options = [ ("grpc.max_send_message_length", -1), ("grpc.max_receive_message_length", -1), ] if not config.url.use_http_proxy: options.append(("grpc.enable_http_proxy", 0)) if (u.scheme in ("https", "grpcs")) or config.ssl: credentials = ssl_channel_credentials( root_certificates=config.ssl.ca, private_key=config.ssl.cert_key, certificate_chain=config.ssl.cert, ) if config.access.token: # The grpc Credential objects do not actually define a formal interface, and are # used interchangeably in the code. # # Additionally there are some incorrect rules in the grpc-stubs typing rules that force # us to work around the type system. credentials = cast( ChannelCredentials, composite_channel_credentials( credentials, metadata_call_credentials(GrpcAuth(config))), ) return secure_channel(u.netloc, credentials, options) else: return insecure_channel(u.netloc, options)
async def fetching(kwargs): self.http_host_port = os.environ['HTTP_GRPC_PORT'] self.headers = kwargs["headers"] self.method = kwargs["method"] self.body = kwargs["body"] self.proxy = kwargs["proxy"] self.url = kwargs["url"] self.clientHello = kwargs["clientHello"] self.httpClientID = kwargs["httpClientID"] channel = aio.insecure_channel('http:' + self.http_host_port) await channel.channel_ready() stub = http_pb2_grpc.HttpClientStub(channel) reply = await stub.GetURL( http_pb2.Request(url=self.url, proxy=self.proxy, headers=self.headers, method=self.method, body=self.body, clientHello=self.clientHello, httpClientID=self.httpClientID)) await channel.close() if reply.error == "": return (reply.status, reply.headers, reply.body) else: return reply.error
async def model_repository_service_stub( grpc_server, grpc_settings: Settings ) -> AsyncGenerator[ModelRepositoryServiceStub, None]: async with aio.insecure_channel( f"{grpc_settings.host}:{grpc_settings.grpc_port}" ) as channel: yield ModelRepositoryServiceStub(channel)
async def inference_service_stub( grpc_server, grpc_settings: Settings ) -> AsyncGenerator[GRPCInferenceServiceStub, None]: async with aio.insecure_channel( f"{grpc_settings.host}:{grpc_settings.grpc_port}" ) as channel: yield GRPCInferenceServiceStub(channel)
async def client_async(server): address = f"{server}:50051" async with aio.insecure_channel(address) as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) request = helloworld_pb2.HelloRequest(name='you') for i in range(REQUEST_COUNT): response = await stub.SayHello(request) return response
async def client_async(): address = f"{os.environ['SERVER']}:50051" async with aio.insecure_channel(address) as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) request = helloworld_pb2.HelloRequest(name='you') for i in range(1000): response = await stub.SayHello(request) print(f"Client received: {response}")
async def run(): # NOTE(gRPC Python Team): .close() is possible on a channel and should be # used in circumstances in which the with statement does not fit the needs # of the code. async with aio.insecure_channel('localhost:50051') as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message)
async def setUp(self): # Create async server self._server = aio.server(options=(('grpc.so_reuseport', 0), )) self._adhoc_handlers = AdhocGenericHandler() self._server.add_generic_rpc_handlers((self._adhoc_handlers, )) port = self._server.add_insecure_port('[::]:0') address = 'localhost:%d' % port await self._server.start() # Create async channel self._channel = aio.insecure_channel(address)
def _start_in_process(self): grpc_aio.init_grpc_aio() app = web.Application() methods = self._getMethods() # connect to product server grpc_channel = grpc_aio.insecure_channel("localhost:50050") aio_stub = self._get_stub_class()(grpc_channel) routes = [] for method in methods: stub_method = getattr(aio_stub, method.name) # construct a partial handler. handler = partial(self._handle_post_unary_unary, aio_stub_method=stub_method, request_type=method.request_cls) routes.append(web.post(method.url, handler)) print(stub_method) app.add_routes(routes) self._app = app web.run_app(app, host="127.0.0.1", port=65000)
async def connect(self) -> None: self.channel = insecure_channel( self.broker, options=[ ("grpc.max_send_message_length", config.liftbridge.max_message_size), ("grpc.max_receive_message_length", config.liftbridge.max_message_size), ("grpc.enable_http_proxy", config.liftbridge.enable_http_proxy), ], ) while True: logger.debug("[%s] Connecting", self.broker) try: await self.wait_for_channel_ready() except ErrorUnavailable as e: logger.debug("[%s] Failed to connect: %s", self.broker, e) await asyncio.sleep(1) continue logger.debug("[%s] Channel is ready", self.broker) self.stub = APIStub(self.channel) return
async def test_aio_from_grpc(self): from grpc import aio # pylint: disable=wrong-import-position channel = aio.insecure_channel('dummy') self.assertIsInstance(channel, aio.Channel)