1 # Copyright 2015 gRPC authors.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Internal utilities for gRPC Python."""
24 from grpc import _common
26 _LOGGER = logging.getLogger(__name__)
28 _DONE_CALLBACK_EXCEPTION_LOG_MESSAGE = (
29 'Exception calling connectivity future "done" callback!')
32 class RpcMethodHandler(
33 collections.namedtuple('_RpcMethodHandler', (
36 'request_deserializer',
37 'response_serializer',
42 )), grpc.RpcMethodHandler):
46 class DictionaryGenericHandler(grpc.ServiceRpcHandler):
48 def __init__(self, service, method_handlers):
50 self._method_handlers = {
51 _common.fully_qualified_method(service, method): method_handler
52 for method, method_handler in six.iteritems(method_handlers)
55 def service_name(self):
58 def service(self, handler_call_details):
59 return self._method_handlers.get(handler_call_details.method)
62 class _ChannelReadyFuture(grpc.Future):
64 def __init__(self, channel):
65 self._condition = threading.Condition()
66 self._channel = channel
69 self._cancelled = False
70 self._done_callbacks = []
72 def _block(self, timeout):
73 until = None if timeout is None else time.time() + timeout
77 raise grpc.FutureCancelledError()
82 self._condition.wait()
84 remaining = until - time.time()
86 raise grpc.FutureTimeoutError()
88 self._condition.wait(timeout=remaining)
90 def _update(self, connectivity):
92 if (not self._cancelled and
93 connectivity is grpc.ChannelConnectivity.READY):
95 self._channel.unsubscribe(self._update)
96 self._condition.notify_all()
97 done_callbacks = tuple(self._done_callbacks)
98 self._done_callbacks = None
102 for done_callback in done_callbacks:
105 except Exception: # pylint: disable=broad-except
106 _LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)
109 with self._condition:
110 if not self._matured:
111 self._cancelled = True
112 self._channel.unsubscribe(self._update)
113 self._condition.notify_all()
114 done_callbacks = tuple(self._done_callbacks)
115 self._done_callbacks = None
119 for done_callback in done_callbacks:
122 except Exception: # pylint: disable=broad-except
123 _LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)
128 with self._condition:
129 return self._cancelled
132 with self._condition:
133 return not self._cancelled and not self._matured
136 with self._condition:
137 return self._cancelled or self._matured
139 def result(self, timeout=None):
142 def exception(self, timeout=None):
145 def traceback(self, timeout=None):
148 def add_done_callback(self, fn):
149 with self._condition:
150 if not self._cancelled and not self._matured:
151 self._done_callbacks.append(fn)
157 with self._condition:
158 self._channel.subscribe(self._update, try_to_connect=True)
161 with self._condition:
162 if not self._cancelled and not self._matured:
163 self._channel.unsubscribe(self._update)
166 def channel_ready_future(channel):
167 ready_future = _ChannelReadyFuture(channel)