本文整理汇总了Python中grpc.Future方法的典型用法代码示例。如果您正苦于以下问题:Python grpc.Future方法的具体用法?Python grpc.Future怎么用?Python grpc.Future使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grpc
的用法示例。
在下文中一共展示了grpc.Future方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call_async
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def call_async(self, method_name, message, call_back=None, timeout=None, is_stub_reuse=True) -> grpc.Future:
if timeout is None:
timeout = conf.GRPC_TIMEOUT
if call_back is None:
call_back = self.print_broadcast_fail
self.__make_stub(is_stub_reuse)
def done_callback(result: _Rendezvous):
if result.code() == grpc.StatusCode.OK:
self.__update_last_succeed_time()
call_back(result)
try:
stub_method = getattr(self.__stub, method_name)
feature_future = stub_method.future(message, timeout)
feature_future.add_done_callback(done_callback)
return feature_future
except Exception as e:
logging.warning(f"gRPC call_async fail method_name({method_name}), message({message}): {e}, "
f"target({self.__target})")
示例2: _handle_grpc_failure
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def _handle_grpc_failure(self, response):
"""Attempts to convert failed responses to a GoogleAdsException object.
Handles failed gRPC responses of by attempting to convert them
to a more readable GoogleAdsException. Certain types of exceptions are
not converted; if the object's trailing metadata does not indicate that
it is a GoogleAdsException, or if it falls under a certain category of
status code, (INTERNAL or RESOURCE_EXHAUSTED). See documentation for
more information about gRPC status codes:
https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
Args:
response: a grpc.Call/grpc.Future instance.
Raises:
GoogleAdsException: If the exception's trailing metadata
indicates that it is a GoogleAdsException.
RpcError: If the exception's is a gRPC exception but the trailing
metadata is empty or is not indicative of a GoogleAdsException,
or if the exception has a status code of INTERNAL or
RESOURCE_EXHAUSTED.
Exception: If not a GoogleAdsException or RpcException the error
will be raised as-is.
"""
raise self._get_error_from_response(response)
示例3: _trace_result
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def _trace_result(self, guarded_span, rpc_info, result):
# If the RPC is called asynchronously, release the guard and add a callback
# so that the span can be finished once the future is done.
if isinstance(result, grpc.Future):
result.add_done_callback(
_make_future_done_callback(guarded_span.release(
), rpc_info, self._log_payloads, self._span_decorator))
return result
response = result
# Handle the case when the RPC is initiated via the with_call
# method and the result is a tuple with the first element as the
# response.
# http://www.grpc.io/grpc/python/grpc.html#grpc.UnaryUnaryMultiCallable.with_call
if isinstance(result, tuple):
response = result[0]
rpc_info.response = response
if self._log_payloads:
guarded_span.span.log_kv({'response': response})
if self._span_decorator is not None:
self._span_decorator(guarded_span.span, rpc_info)
return result
示例4: _trace_result
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def _trace_result(self, guarded_span, rpc_info, result):
# If the RPC is called asynchronously, release the guard and add a
# callback so that the span can be finished once the future is done.
if isinstance(result, grpc.Future):
result.add_done_callback(
_make_future_done_callback(guarded_span.release(), rpc_info)
)
return result
response = result
# Handle the case when the RPC is initiated via the with_call
# method and the result is a tuple with the first element as the
# response.
# http://www.grpc.io/grpc/python/grpc.html#grpc.UnaryUnaryMultiCallable.with_call
if isinstance(result, tuple):
response = result[0]
rpc_info.response = response
return result
示例5: test_queue_rpc
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def test_queue_rpc(self):
loop = self._make_one()
callback = mock.Mock(spec=())
rpc = mock.Mock(spec=grpc.Future)
loop.queue_rpc(rpc, callback)
assert list(loop.rpcs.values()) == [callback]
rpc_callback = rpc.add_done_callback.call_args[0][0]
rpc_callback(rpc)
rpc_id, rpc_result = loop.rpc_results.get()
assert rpc_result is rpc
assert loop.rpcs[rpc_id] is callback
示例6: test_run0_rpc
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def test_run0_rpc(self):
rpc = mock.Mock(spec=grpc.Future)
callback = mock.Mock(spec=())
loop = self._make_one()
loop.rpcs["foo"] = callback
loop.rpc_results.put(("foo", rpc))
loop.run0()
assert len(loop.rpcs) == 0
assert loop.rpc_results.empty()
callback.assert_called_once_with(rpc)
示例7: exception
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def exception(self):
"""Calls :meth:`grpc.Future.exception` on :attr:`future`."""
# GRPC will actually raise FutureCancelledError.
# We'll translate that to our own Cancelled exception and *return* it,
# which is far more polite for a method that *returns exceptions*.
try:
return self.future.exception()
except grpc.FutureCancelledError:
return exceptions.Cancelled()
示例8: result
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def result(self):
"""Calls :meth:`grpc.Future.result` on :attr:`future`."""
return self.future.result()
示例9: cancel
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def cancel(self):
"""Calls :meth:`grpc.Future.cancel` on attr:`cancel`."""
return self.future.cancel()
示例10: _trace_future_exception
# 需要导入模块: import grpc [as 别名]
# 或者: from grpc import Future [as 别名]
def _trace_future_exception(self, response):
# Trace the exception for a grpc.Future if any
exception = response.exception()
if exception is not None:
exception = str(exception)
self.tracer.add_attribute_to_current_span(
attribute_key=attributes_helper.COMMON_ATTRIBUTES.get(
ATTRIBUTE_ERROR_MESSAGE),
attribute_value=exception)