python类RpcError()的实例源码

test_api_callable.py 文件源码 项目:gax-python 作者: googleapis 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_wrap_value_error(self):
        from google.gax.errors import InvalidArgumentError

        invalid_attribute_exc = grpc.RpcError()
        invalid_attribute_exc.code = lambda: grpc.StatusCode.INVALID_ARGUMENT

        def value_error_func(*dummy_args, **dummy_kwargs):
            raise invalid_attribute_exc

        value_error_callable = api_callable.create_api_call(
            value_error_func, _CallSettings())

        with self.assertRaises(ValueError) as exc_info:
            value_error_callable(None)

        self.assertIsInstance(exc_info.exception, InvalidArgumentError)
        self.assertEqual(exc_info.exception.args, (u'RPC failed',))
        self.assertIs(exc_info.exception.cause, invalid_attribute_exc)
client.py 文件源码 项目:python-etcd3 作者: kragniz 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _handle_errors(f):
    if inspect.isgeneratorfunction(f):
        def handler(*args, **kwargs):
            try:
                for data in f(*args, **kwargs):
                    yield data
            except grpc.RpcError as exc:
                _translate_exception(exc)
    else:
        def handler(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except grpc.RpcError as exc:
                _translate_exception(exc)

    return functools.wraps(f)(handler)
transcribe_streaming.py 文件源码 项目:appbackendapi 作者: codesdk 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def main():
    service = cloud_speech_pb2.SpeechStub(
        make_channel('speech.googleapis.com', 443))

    # For streaming audio from the microphone, there are three threads.
    # First, a thread that collects audio data as it comes in
    with record_audio(RATE, CHUNK) as buffered_audio_data:
        # Second, a thread that sends requests with that data
        requests = request_stream(buffered_audio_data, RATE)
        # Third, a thread that listens for transcription responses
        recognize_stream = service.StreamingRecognize(
            requests, DEADLINE_SECS)

        # Exit things cleanly on interrupt
        signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel())

        # Now, put the transcription responses to use.
        try:
            listen_print_loop(recognize_stream)

            recognize_stream.cancel()
        except grpc.RpcError:
            # This happens because of the interrupt handler
            pass
server_test.py 文件源码 项目:PowerSpikeGG 作者: PowerSpikeGG 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_bad_request(self):
        """Check if the server correctly raise error on bad request."""
        with self.assertRaises(grpc.RpcError):
            self.stub.Match(service_pb2.MatchRequest(id=4242))
        with self.assertRaises(grpc.RpcError):
            self.stub.Match(service_pb2.MatchRequest())
        with self.assertRaises(grpc.RpcError):
            self.stub.Match(service_pb2.MatchRequest(region=constants_pb2.EUW))
grpc.py 文件源码 项目:gax-python 作者: googleapis 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def exc_to_code(exc):
    """Retrieves the status code from an exception"""
    if not isinstance(exc, RpcError):
        return None
    else:
        try:
            return exc.code()
        except AttributeError:
            return None
ok_google.py 文件源码 项目:ok_google 作者: respeaker 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def is_grpc_error_unavailable(e):
        is_grpc_error = isinstance(e, grpc.RpcError)
        if is_grpc_error and (e.code() == grpc.StatusCode.UNAVAILABLE):
            logging.error('grpc unavailable error: %s', e)
            return True
        return False
speech.py 文件源码 项目:aiyprojects-raspbian 作者: google 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def do_request(self):
        """Establishes a connection and starts sending audio to the cloud
        endpoint. Responses are handled by the subclass until one returns a
        result.

        Returns:
            namedtuple with the following fields:
                transcript: string with transcript of user query
                response_audio: optionally, an audio response from the server

        Raises speech.Error on error.
        """
        try:
            service = self._make_service(self._channel_factory.make_channel())

            response_stream = self._create_response_stream(
                service, self._request_stream(), self.DEADLINE_SECS)

            if self._audio_logging_enabled:
                self._start_logging_request()

            return self._handle_response_stream(response_stream)
        except (
                google.auth.exceptions.GoogleAuthError,
                grpc.RpcError,
        ) as exc:
            raise Error('Exception in speech request') from exc
_speech.py 文件源码 项目:aiyprojects-raspbian 作者: google 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def do_request(self):
        """Establishes a connection and starts sending audio to the cloud
        endpoint. Responses are handled by the subclass until one returns a
        result.

        Returns:
            namedtuple with the following fields:
                transcript: string with transcript of user query
                response_audio: optionally, an audio response from the server

        Raises speech.Error on error.
        """
        try:
            service = self._make_service(self._channel_factory.make_channel())

            response_stream = self._create_response_stream(
                service, self._request_stream(), self.DEADLINE_SECS)

            if self._audio_logging_enabled:
                self._start_logging_request()

            return self._handle_response_stream(response_stream)
        except (
                google.auth.exceptions.GoogleAuthError,
                grpc.RpcError,
        ) as exc:
            raise Error('Exception in speech request') from exc
ps_bridge_connection.py 文件源码 项目:relaax 作者: deeplearninc 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _init_ten_times(self):
        message = bridge_pb2.NullMessage()
        for _ in range(9):
            try:
                return self._stub.Init(message)
            except grpc.RpcError as e:
                pass
        return self._stub.Init(message)
metrics_bridge_connection.py 文件源码 项目:relaax 作者: deeplearninc 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def send(self, method_name, message_factory):
        if self._stub is None:
            self._stub = bridge_pb2.BridgeStub(grpc.insecure_channel('%s:%d' % self._server))
            for _ in range(9):
                method = getattr(self._stub, method_name)
                message = message_factory()
                try:
                    return method(message)
                except grpc.RpcError as e:
                    pass
        return getattr(self._stub, method_name)(message_factory())
watch.py 文件源码 项目:python-etcd3 作者: kragniz 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def run(self):
        try:
            for response in self._watch_response_iterator:
                if response.created:
                    self._watch_id_callbacks[response.watch_id] = \
                        self._callback
                    self._watch_id_queue.put(response.watch_id)

                callback = self._watch_id_callbacks.get(response.watch_id)
                if callback:
                    # The watcher can be safely reused, but adding a new event
                    # to indicate that the revision is already compacted
                    # requires api change which would break all users of this
                    # module. So, raising an exception if a watcher is still
                    # alive. The caller has to create a new client instance to
                    # recover would break all users of this module.
                    if response.compact_revision != 0:
                        callback(etcd3_exceptions.RevisionCompactedError(
                            response.compact_revision))
                        self.cancel(response.watch_id)
                        continue
                    for event in response.events:
                        callback(events.new_event(event))
        except grpc.RpcError as e:
            self.stop()
            if self._watch_id_callbacks:
                for callback in self._watch_id_callbacks.values():
                    callback(e)
test_etcd3.py 文件源码 项目:python-etcd3 作者: kragniz 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_grpc_exception_on_unknown_code(self, etcd):
        exception = self.MockedException(grpc.StatusCode.DATA_LOSS)
        kv_mock = mock.MagicMock()
        kv_mock.Range.side_effect = exception
        etcd.kvstub = kv_mock

        with pytest.raises(grpc.RpcError):
            etcd.get("foo")
test_etcd3.py 文件源码 项目:python-etcd3 作者: kragniz 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_compact(self, etcd):
        etcd.compact(3)
        with pytest.raises(grpc.RpcError):
            etcd.compact(3)
pushtotalk.py 文件源码 项目:assistant-sdk-python 作者: googlesamples 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def is_grpc_error_unavailable(e):
        is_grpc_error = isinstance(e, grpc.RpcError)
        if is_grpc_error and (e.code() == grpc.StatusCode.UNAVAILABLE):
            logging.error('grpc unavailable error: %s', e)
            return True
        return False
prod_client.py 文件源码 项目:tfserving_predict_client 作者: epigramai 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def predict(self, request_data, request_timeout=10):

        logger.info('Sending request to tfserving model')
        logger.info('Model name: ' + str(self.model_name))
        logger.info('Model version: ' + str(self.model_version))
        logger.info('Host: ' + str(self.host))

        tensor_shape = request_data.shape

        if self.model_name == 'incv4' or self.model_name == 'res152':
            features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data, shape=tensor_shape)
        else:
            features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data,
                                                                      dtype=tf.float32, shape=tensor_shape)

        # Create gRPC client and request
        channel = grpc.insecure_channel(self.host)
        stub = PredictionServiceStub(channel)
        request = PredictRequest()

        request.model_spec.name = self.model_name

        if self.model_version > 0:
            request.model_spec.version.value = self.model_version

        request.inputs['inputs'].CopyFrom(features_tensor_proto)

        try:
            result = stub.Predict(request, timeout=request_timeout)
            logger.info('Got scores with len: ' + str(len(list(result.outputs['scores'].float_val))))
            return list(result.outputs['scores'].float_val)
        except RpcError as e:
            logger.error(e)
            logger.error('Prediction failed!')
mock_client.py 文件源码 项目:tfserving_predict_client 作者: epigramai 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def predict(self, request_data, request_timeout=10):

        logger.info('Sending request to tfserving model')
        logger.info('Model name: ' + str(self.model_name))
        logger.info('Model version: ' + str(self.model_version))
        logger.info('Host: ' + str(self.host))

        tensor_shape = request_data.shape

        if self.model_name == 'incv4' or self.model_name == 'res152':
            features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data, shape=tensor_shape)
        else:
            features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data,
                                                                      dtype=tf.float32, shape=tensor_shape)

        # Create gRPC client and request
        channel = grpc.insecure_channel(self.host)
        stub = PredictionServiceStub(channel)
        request = PredictRequest()

        request.model_spec.name = self.model_name

        if self.model_version > 0:
            request.model_spec.version.value = self.model_version

        request.inputs['inputs'].CopyFrom(features_tensor_proto)

        try:
            result = stub.Predict(request, timeout=request_timeout)
            logger.debug('Predicted scores with len: ' + str(len(list(result.outputs['scores'].float_val))))
            return list(result.outputs['scores'].float_val)
        except RpcError as e:
            logger.warning(e)
            logger.warning('Prediction failed. Mock client will return empty prediction of length: '
                           + str(self.num_scores))
            return [0] * self.num_scores
cluster.py 文件源码 项目:aioetcd3 作者: gaopeiliang 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def member_healthy(self, members=None):

        if not members:
            members = await self.member_list()
            members = [m.clientURLs for m in members]

        health_members = []
        unhealth_members = []
        for m in members:

            m = [u.rpartition("//")[2] for u in m]
            server_endpoint = ipv4_endpoints(m)

            if self._credentials:
                channel = aiogrpc.secure_channel(server_endpoint, self._credentials, options=self._options,
                                                 loop=self._loop, executor=self._executor,
                                                 standalone_pool_for_streaming=True)
            else:
                channel = aiogrpc.insecure_channel(server_endpoint, options=self._options, loop=self._loop,
                                                   executor=self._executor, standalone_pool_for_streaming=True)

            maintenance = Maintenance(channel=channel, timeout=2)
            try:
                await maintenance.status()
            except grpc.RpcError:
                unhealth_members.append(m)
            else:
                health_members.append(m)

        return health_members, unhealth_members
clusterspecgenerator_client.py 文件源码 项目:yarntf 作者: tobiajo 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def register_container(self, application_id, ip, port, job_name, task_index):
        container = csg.Container()
        container.applicationId = application_id
        container.ip = ip
        container.port = port
        container.jobName = job_name
        container.taskIndex = task_index
        request = csg.RegisterContainerRequest(container=container)
        try:
            self.stub.RegisterContainer(request)
        except grpc.RpcError:
            return False
        return True
clusterspecgenerator_client.py 文件源码 项目:yarntf 作者: tobiajo 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def get_cluster_spec(self, application_id):
        request = csg.GetClusterSpecRequest()
        request.applicationId = application_id
        try:
            reply = self.stub.GetClusterSpec(request)
        except grpc.RpcError:
            return None
        return reply.clusterSpec
pushbutton.py 文件源码 项目:GassistPi 作者: shivasiddharth 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def is_grpc_error_unavailable(e):
        is_grpc_error = isinstance(e, grpc.RpcError)
        if is_grpc_error and (e.code() == grpc.StatusCode.UNAVAILABLE):
            logging.error('grpc unavailable error: %s', e)
            return True
        return False
client.py 文件源码 项目:python-speech-recog 作者: whittlbc 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def listen(self):
        service = cloud_speech_pb2.SpeechStub(self.make_channel('speech.googleapis.com', 443))

        # For streaming audio from the microphone, there are three threads.
        # First, a thread that collects audio data as it comes in
        with self.record_audio(self.RATE, self.CHUNK) as buff:
            # Second, a thread that sends requests with that data
            overlap_buffer = collections.deque(maxlen=self.SECS_OVERLAP * self.RATE / self.CHUNK)
            requests = self.request_stream(self._audio_data_generator(buff, overlap_buffer), self.RATE)
            # Third, a thread that listens for transcription responses
            recognize_stream = service.StreamingRecognize(
                requests, self.DEADLINE_SECS)

            # Exit things cleanly on interrupt
            signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel())

            # Now, put the transcription responses to use.
            try:
                while True:
                    self.listen_print_loop(recognize_stream, buff)

                    # Discard this stream and create a new one.
                    # Note: calling .cancel() doesn't immediately raise an RpcError
                    # - it only raises when the iterator's next() is requested
                    recognize_stream.cancel()

                    requests = self.request_stream(self._audio_data_generator(
                        buff, overlap_buffer), self.RATE)
                    # Third, a thread that listens for transcription responses
                    recognize_stream = service.StreamingRecognize(
                        requests, self.DEADLINE_SECS)

            except grpc.RpcError:
                # This happens because of the interrupt handler
                pass
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def testUnaryUnaryOpenTracing(self):
        multi_callable = self._service.unary_unary_multi_callable
        request = b'\x01'
        self.assertRaises(grpc.RpcError, multi_callable, request)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def testUnaryUnaryOpenTracingWithCall(self):
        multi_callable = self._service.unary_unary_multi_callable
        request = b'\x01'
        self.assertRaises(grpc.RpcError, multi_callable.with_call, request)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def testUnaryStreamOpenTracing(self):
        multi_callable = self._service.unary_stream_multi_callable
        request = b'\x01'
        response = multi_callable(request)
        self.assertRaises(grpc.RpcError, list, response)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def testStreamUnaryOpenTracing(self):
        multi_callable = self._service.stream_unary_multi_callable
        requests = [b'\x01', b'\x02']
        self.assertRaises(grpc.RpcError, multi_callable, iter(requests))

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def testStreamUnaryOpenTracingWithCall(self):
        multi_callable = self._service.stream_unary_multi_callable
        requests = [b'\x01', b'\x02']
        self.assertRaises(grpc.RpcError, multi_callable.with_call,
                          iter(requests))

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testUnaryUnaryOpenTracing(self):
        multi_callable = self._service.unary_unary_multi_callable
        request = b'\x01'
        self.assertRaises(grpc.RpcError, multi_callable, request)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testUnaryUnaryOpenTracingWithCall(self):
        multi_callable = self._service.unary_unary_multi_callable
        request = b'\x01'
        self.assertRaises(grpc.RpcError, multi_callable.with_call, request)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def testUnaryStreamOpenTracing(self):
        multi_callable = self._service.unary_stream_multi_callable
        request = b'\x01'
        response = multi_callable(request)
        self.assertRaises(grpc.RpcError, list, response)

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))
test_opentracing.py 文件源码 项目:grpc-opentracing 作者: grpc-ecosystem 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def testStreamUnaryOpenTracing(self):
        multi_callable = self._service.stream_unary_multi_callable
        requests = [b'\x01', b'\x02']
        self.assertRaises(grpc.RpcError, multi_callable, iter(requests))

        span0 = self._tracer.get_span(0)
        self.assertIsNotNone(span0)
        self.assertTrue(span0.get_tag('error'))

        span1 = self._tracer.get_span(1)
        self.assertIsNotNone(span1)
        self.assertTrue(span1.get_tag('error'))


问题


面经


文章

微信
公众号

扫码关注公众号