Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/grpc/beta/utilities.py
7771 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"""Utilities for the gRPC Python Beta API."""1415import threading16import time1718# implementations is referenced from specification in this module.19from grpc.beta import implementations # pylint: disable=unused-import20from grpc.beta import interfaces21from grpc.framework.foundation import callable_util22from grpc.framework.foundation import future2324_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE = (25'Exception calling connectivity future "done" callback!')262728class _ChannelReadyFuture(future.Future):2930def __init__(self, channel):31self._condition = threading.Condition()32self._channel = channel3334self._matured = False35self._cancelled = False36self._done_callbacks = []3738def _block(self, timeout):39until = None if timeout is None else time.time() + timeout40with self._condition:41while True:42if self._cancelled:43raise future.CancelledError()44elif self._matured:45return46else:47if until is None:48self._condition.wait()49else:50remaining = until - time.time()51if remaining < 0:52raise future.TimeoutError()53else:54self._condition.wait(timeout=remaining)5556def _update(self, connectivity):57with self._condition:58if (not self._cancelled and59connectivity is interfaces.ChannelConnectivity.READY):60self._matured = True61self._channel.unsubscribe(self._update)62self._condition.notify_all()63done_callbacks = tuple(self._done_callbacks)64self._done_callbacks = None65else:66return6768for done_callback in done_callbacks:69callable_util.call_logging_exceptions(70done_callback, _DONE_CALLBACK_EXCEPTION_LOG_MESSAGE, self)7172def cancel(self):73with self._condition:74if not self._matured:75self._cancelled = True76self._channel.unsubscribe(self._update)77self._condition.notify_all()78done_callbacks = tuple(self._done_callbacks)79self._done_callbacks = None80else:81return False8283for done_callback in done_callbacks:84callable_util.call_logging_exceptions(85done_callback, _DONE_CALLBACK_EXCEPTION_LOG_MESSAGE, self)8687return True8889def cancelled(self):90with self._condition:91return self._cancelled9293def running(self):94with self._condition:95return not self._cancelled and not self._matured9697def done(self):98with self._condition:99return self._cancelled or self._matured100101def result(self, timeout=None):102self._block(timeout)103return None104105def exception(self, timeout=None):106self._block(timeout)107return None108109def traceback(self, timeout=None):110self._block(timeout)111return None112113def add_done_callback(self, fn):114with self._condition:115if not self._cancelled and not self._matured:116self._done_callbacks.append(fn)117return118119fn(self)120121def start(self):122with self._condition:123self._channel.subscribe(self._update, try_to_connect=True)124125def __del__(self):126with self._condition:127if not self._cancelled and not self._matured:128self._channel.unsubscribe(self._update)129130131def channel_ready_future(channel):132"""Creates a future.Future tracking when an implementations.Channel is ready.133134Cancelling the returned future.Future does not tell the given135implementations.Channel to abandon attempts it may have been making to136connect; cancelling merely deactivates the return future.Future's137subscription to the given implementations.Channel's connectivity.138139Args:140channel: An implementations.Channel.141142Returns:143A future.Future that matures when the given Channel has connectivity144interfaces.ChannelConnectivity.READY.145"""146ready_future = _ChannelReadyFuture(channel)147ready_future.start()148return ready_future149150151