def run_client(channel_compression, call_compression, target): with grpc.insecure_channel( target, compression=channel_compression) as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) response = stub.SayHello( helloworld_pb2.HelloRequest(name='you'), compression=call_compression, wait_for_ready=True) print("Response: {}".format(response))
def send_rpc(channel): stub = helloworld_pb2_grpc.GreeterStub(channel) request = helloworld_pb2.HelloRequest(name='you') try: response = stub.SayHello(request) except grpc.RpcError as rpc_error: _LOGGER.error('Received error: %s', rpc_error) return rpc_error else: _LOGGER.info('Received message: %s', response) return response
def process(stub, wait_for_ready=None): try: response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), wait_for_ready=wait_for_ready) message = response.message except grpc.RpcError as rpc_error: assert rpc_error.code() == grpc.StatusCode.UNAVAILABLE assert not wait_for_ready message = rpc_error else: assert wait_for_ready _LOGGER.info("Wait-for-ready %s, client received: %s", "enabled" if wait_for_ready else "disabled", message)
def process(stub): try: response = stub.SayHello(helloworld_pb2.HelloRequest(name='Alice')) _LOGGER.info('Call success: %s', response.message) except grpc.RpcError as rpc_error: _LOGGER.error('Call failure: %s', rpc_error) status = rpc_status.from_call(rpc_error) for detail in status.details: if detail.Is(error_details_pb2.QuotaFailure.DESCRIPTOR): info = error_details_pb2.QuotaFailure() detail.Unpack(info) _LOGGER.error('Quota failure: %s', info) else: raise RuntimeError('Unexpected failure: %s' % detail)
def run(addr, n): with grpc.insecure_channel(addr) as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) request = helloworld_pb2.HelloRequest(name='you') for _ in range(n): process(stub, request)