Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/grpc/_utilities.py
7763 views
1
# Copyright 2015 gRPC authors.
2
#
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
6
#
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
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."""
15
16
import collections
17
import logging
18
import threading
19
import time
20
21
import grpc
22
from grpc import _common
23
import six
24
25
_LOGGER = logging.getLogger(__name__)
26
27
_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE = (
28
'Exception calling connectivity future "done" callback!')
29
30
31
class RpcMethodHandler(
32
collections.namedtuple('_RpcMethodHandler', (
33
'request_streaming',
34
'response_streaming',
35
'request_deserializer',
36
'response_serializer',
37
'unary_unary',
38
'unary_stream',
39
'stream_unary',
40
'stream_stream',
41
)), grpc.RpcMethodHandler):
42
pass
43
44
45
class DictionaryGenericHandler(grpc.ServiceRpcHandler):
46
47
def __init__(self, service, method_handlers):
48
self._name = service
49
self._method_handlers = {
50
_common.fully_qualified_method(service, method): method_handler
51
for method, method_handler in six.iteritems(method_handlers)
52
}
53
54
def service_name(self):
55
return self._name
56
57
def service(self, handler_call_details):
58
return self._method_handlers.get(handler_call_details.method)
59
60
61
class _ChannelReadyFuture(grpc.Future):
62
63
def __init__(self, channel):
64
self._condition = threading.Condition()
65
self._channel = channel
66
67
self._matured = False
68
self._cancelled = False
69
self._done_callbacks = []
70
71
def _block(self, timeout):
72
until = None if timeout is None else time.time() + timeout
73
with self._condition:
74
while True:
75
if self._cancelled:
76
raise grpc.FutureCancelledError()
77
elif self._matured:
78
return
79
else:
80
if until is None:
81
self._condition.wait()
82
else:
83
remaining = until - time.time()
84
if remaining < 0:
85
raise grpc.FutureTimeoutError()
86
else:
87
self._condition.wait(timeout=remaining)
88
89
def _update(self, connectivity):
90
with self._condition:
91
if (not self._cancelled and
92
connectivity is grpc.ChannelConnectivity.READY):
93
self._matured = True
94
self._channel.unsubscribe(self._update)
95
self._condition.notify_all()
96
done_callbacks = tuple(self._done_callbacks)
97
self._done_callbacks = None
98
else:
99
return
100
101
for done_callback in done_callbacks:
102
try:
103
done_callback(self)
104
except Exception: # pylint: disable=broad-except
105
_LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)
106
107
def cancel(self):
108
with self._condition:
109
if not self._matured:
110
self._cancelled = True
111
self._channel.unsubscribe(self._update)
112
self._condition.notify_all()
113
done_callbacks = tuple(self._done_callbacks)
114
self._done_callbacks = None
115
else:
116
return False
117
118
for done_callback in done_callbacks:
119
try:
120
done_callback(self)
121
except Exception: # pylint: disable=broad-except
122
_LOGGER.exception(_DONE_CALLBACK_EXCEPTION_LOG_MESSAGE)
123
124
return True
125
126
def cancelled(self):
127
with self._condition:
128
return self._cancelled
129
130
def running(self):
131
with self._condition:
132
return not self._cancelled and not self._matured
133
134
def done(self):
135
with self._condition:
136
return self._cancelled or self._matured
137
138
def result(self, timeout=None):
139
self._block(timeout)
140
141
def exception(self, timeout=None):
142
self._block(timeout)
143
144
def traceback(self, timeout=None):
145
self._block(timeout)
146
147
def add_done_callback(self, fn):
148
with self._condition:
149
if not self._cancelled and not self._matured:
150
self._done_callbacks.append(fn)
151
return
152
153
fn(self)
154
155
def start(self):
156
with self._condition:
157
self._channel.subscribe(self._update, try_to_connect=True)
158
159
def __del__(self):
160
with self._condition:
161
if not self._cancelled and not self._matured:
162
self._channel.unsubscribe(self._update)
163
164
165
def channel_ready_future(channel):
166
ready_future = _ChannelReadyFuture(channel)
167
ready_future.start()
168
return ready_future
169
170