Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/grpc/_utilities.py
7763 views
# Copyright 2015 gRPC authors.1#2# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13"""Internal utilities for gRPC Python."""1415import collections16import logging17import threading18import time1920import grpc21from grpc import _common22import six2324_LOGGER = logging.getLogger(__name__)2526_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE = (27'Exception calling connectivity future "done" callback!')282930class RpcMethodHandler(31collections.namedtuple('_RpcMethodHandler', (32'request_streaming',33'response_streaming',34'request_deserializer',35'response_serializer',36'unary_unary',37'unary_stream',38'stream_unary',39'stream_stream',40)), grpc.RpcMethodHandler):41pass424344class DictionaryGenericHandler(grpc.ServiceRpcHandler):4546def __init__(self, service, method_handlers):47self._name = service48self._method_handlers = {49_common.fully_qualified_method(service, method): method_handler50for method, method_handler in six.iteritems(method_handlers)51}5253def service_name(self):54return self._name5556def service(self, handler_call_details):57return self._method_handlers.get(handler_call_details.method)585960class _ChannelReadyFuture(grpc.Future):6162def __init__(self, channel):63self._condition = threading.Condition()64self._channel = channel6566self._matured = False67self._cancelled = False68self._done_callbacks = []6970def _block(self, timeout):71until = None if timeout is None else time.time() + timeout72with self._condition:73while True:74if self._cancelled:75raise grpc.FutureCancelledError()76elif self._matured:77return78else:79if until is None:80self._condition.wait()81else:82remaining = until - time.time()83if remaining < 0:84raise grpc.FutureTimeoutError()85else:86self._condition.wait(timeout=remaining)8788def _update(self, connectivity):89with self._condition:90if (not self._cancelled and91connectivity is grpc.ChannelConnectivity.READY):92self._matured = True93self._channel.unsubscribe(self._update)94self._condition.notify_all()95done_callbacks = tuple(self._done_callbacks)96self._done_callbacks = None97else:98return99100for done_callback in done_callbacks:101try:102done_callback(self)103except Exception: # pylint: disable=broad-except104_LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)105106def cancel(self):107with self._condition:108if not self._matured:109self._cancelled = True110self._channel.unsubscribe(self._update)111self._condition.notify_all()112done_callbacks = tuple(self._done_callbacks)113self._done_callbacks = None114else:115return False116117for done_callback in done_callbacks:118try:119done_callback(self)120except Exception: # pylint: disable=broad-except121_LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)122123return True124125def cancelled(self):126with self._condition:127return self._cancelled128129def running(self):130with self._condition:131return not self._cancelled and not self._matured132133def done(self):134with self._condition:135return self._cancelled or self._matured136137def result(self, timeout=None):138self._block(timeout)139140def exception(self, timeout=None):141self._block(timeout)142143def traceback(self, timeout=None):144self._block(timeout)145146def add_done_callback(self, fn):147with self._condition:148if not self._cancelled and not self._matured:149self._done_callbacks.append(fn)150return151152fn(self)153154def start(self):155with self._condition:156self._channel.subscribe(self._update, try_to_connect=True)157158def __del__(self):159with self._condition:160if not self._cancelled and not self._matured:161self._channel.unsubscribe(self._update)162163164def channel_ready_future(channel):165ready_future = _ChannelReadyFuture(channel)166ready_future.start()167return ready_future168169170