1 # Copyright 2020 The 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 """Abstract base classes for Channel objects and Multicallable objects."""
17 from typing import Any, Optional
21 from . import _base_call
22 from ._typing import (DeserializingFunction, RequestIterableType,
24 from ._metadata import Metadata
27 class UnaryUnaryMultiCallable(abc.ABC):
28 """Enables asynchronous invocation of a unary-call RPC."""
35 timeout: Optional[float] = None,
36 metadata: Optional[Metadata] = None,
37 credentials: Optional[grpc.CallCredentials] = None,
38 wait_for_ready: Optional[bool] = None,
39 compression: Optional[grpc.Compression] = None
40 ) -> _base_call.UnaryUnaryCall:
41 """Asynchronously invokes the underlying RPC.
44 request: The request value for the RPC.
45 timeout: An optional duration of time in seconds to allow
47 metadata: Optional :term:`metadata` to be transmitted to the
48 service-side of the RPC.
49 credentials: An optional CallCredentials for the RPC. Only valid for
51 wait_for_ready: This is an EXPERIMENTAL argument. An optional
52 flag to enable :term:`wait_for_ready` mechanism.
53 compression: An element of grpc.compression, e.g.
54 grpc.compression.Gzip. This is an EXPERIMENTAL option.
57 A UnaryUnaryCall object.
60 RpcError: Indicates that the RPC terminated with non-OK status. The
61 raised RpcError will also be a Call for the RPC affording the RPC's
62 metadata, status code, and details.
66 class UnaryStreamMultiCallable(abc.ABC):
67 """Enables asynchronous invocation of a server-streaming RPC."""
74 timeout: Optional[float] = None,
75 metadata: Optional[Metadata] = None,
76 credentials: Optional[grpc.CallCredentials] = None,
77 wait_for_ready: Optional[bool] = None,
78 compression: Optional[grpc.Compression] = None
79 ) -> _base_call.UnaryStreamCall:
80 """Asynchronously invokes the underlying RPC.
83 request: The request value for the RPC.
84 timeout: An optional duration of time in seconds to allow
86 metadata: Optional :term:`metadata` to be transmitted to the
87 service-side of the RPC.
88 credentials: An optional CallCredentials for the RPC. Only valid for
90 wait_for_ready: This is an EXPERIMENTAL argument. An optional
91 flag to enable :term:`wait_for_ready` mechanism.
92 compression: An element of grpc.compression, e.g.
93 grpc.compression.Gzip. This is an EXPERIMENTAL option.
96 A UnaryStreamCall object.
99 RpcError: Indicates that the RPC terminated with non-OK status. The
100 raised RpcError will also be a Call for the RPC affording the RPC's
101 metadata, status code, and details.
105 class StreamUnaryMultiCallable(abc.ABC):
106 """Enables asynchronous invocation of a client-streaming RPC."""
111 request_iterator: Optional[RequestIterableType] = None,
112 timeout: Optional[float] = None,
113 metadata: Optional[Metadata] = None,
114 credentials: Optional[grpc.CallCredentials] = None,
115 wait_for_ready: Optional[bool] = None,
116 compression: Optional[grpc.Compression] = None
117 ) -> _base_call.StreamUnaryCall:
118 """Asynchronously invokes the underlying RPC.
121 request_iterator: An optional async iterable or iterable of request
122 messages for the RPC.
123 timeout: An optional duration of time in seconds to allow
125 metadata: Optional :term:`metadata` to be transmitted to the
126 service-side of the RPC.
127 credentials: An optional CallCredentials for the RPC. Only valid for
129 wait_for_ready: This is an EXPERIMENTAL argument. An optional
130 flag to enable :term:`wait_for_ready` mechanism.
131 compression: An element of grpc.compression, e.g.
132 grpc.compression.Gzip. This is an EXPERIMENTAL option.
135 A StreamUnaryCall object.
138 RpcError: Indicates that the RPC terminated with non-OK status. The
139 raised RpcError will also be a Call for the RPC affording the RPC's
140 metadata, status code, and details.
144 class StreamStreamMultiCallable(abc.ABC):
145 """Enables asynchronous invocation of a bidirectional-streaming RPC."""
150 request_iterator: Optional[RequestIterableType] = None,
151 timeout: Optional[float] = None,
152 metadata: Optional[Metadata] = None,
153 credentials: Optional[grpc.CallCredentials] = None,
154 wait_for_ready: Optional[bool] = None,
155 compression: Optional[grpc.Compression] = None
156 ) -> _base_call.StreamStreamCall:
157 """Asynchronously invokes the underlying RPC.
160 request_iterator: An optional async iterable or iterable of request
161 messages for the RPC.
162 timeout: An optional duration of time in seconds to allow
164 metadata: Optional :term:`metadata` to be transmitted to the
165 service-side of the RPC.
166 credentials: An optional CallCredentials for the RPC. Only valid for
168 wait_for_ready: This is an EXPERIMENTAL argument. An optional
169 flag to enable :term:`wait_for_ready` mechanism.
170 compression: An element of grpc.compression, e.g.
171 grpc.compression.Gzip. This is an EXPERIMENTAL option.
174 A StreamStreamCall object.
177 RpcError: Indicates that the RPC terminated with non-OK status. The
178 raised RpcError will also be a Call for the RPC affording the RPC's
179 metadata, status code, and details.
183 class Channel(abc.ABC):
184 """Enables asynchronous RPC invocation as a client.
186 Channel objects implement the Asynchronous Context Manager (aka. async
187 with) type, although they are not supportted to be entered and exited
192 async def __aenter__(self):
193 """Starts an asynchronous context manager.
196 Channel the channel that was instantiated.
200 async def __aexit__(self, exc_type, exc_val, exc_tb):
201 """Finishes the asynchronous context manager by closing the channel.
203 Still active RPCs will be cancelled.
207 async def close(self, grace: Optional[float] = None):
208 """Closes this Channel and releases all resources held by it.
210 This method immediately stops the channel from executing new RPCs in
213 If a grace period is specified, this method wait until all active
214 RPCs are finshed, once the grace period is reached the ones that haven't
215 been terminated are cancelled. If a grace period is not specified
216 (by passing None for grace), all existing RPCs are cancelled immediately.
218 This method is idempotent.
223 try_to_connect: bool = False) -> grpc.ChannelConnectivity:
224 """Checks the connectivity state of a channel.
226 This is an EXPERIMENTAL API.
228 If the channel reaches a stable connectivity state, it is guaranteed
229 that the return value of this function will eventually converge to that
233 try_to_connect: a bool indicate whether the Channel should try to
234 connect to peer or not.
236 Returns: A ChannelConnectivity object.
240 async def wait_for_state_change(
242 last_observed_state: grpc.ChannelConnectivity,
244 """Waits for a change in connectivity state.
246 This is an EXPERIMENTAL API.
248 The function blocks until there is a change in the channel connectivity
249 state from the "last_observed_state". If the state is already
250 different, this function will return immediately.
252 There is an inherent race between the invocation of
253 "Channel.wait_for_state_change" and "Channel.get_state". The state can
254 change arbitrary many times during the race, so there is no way to
255 observe every state transition.
257 If there is a need to put a timeout for this function, please refer to
261 last_observed_state: A grpc.ChannelConnectivity object representing
262 the last known state.
266 async def channel_ready(self) -> None:
267 """Creates a coroutine that blocks until the Channel is READY."""
273 request_serializer: Optional[SerializingFunction] = None,
274 response_deserializer: Optional[DeserializingFunction] = None
275 ) -> UnaryUnaryMultiCallable:
276 """Creates a UnaryUnaryMultiCallable for a unary-unary method.
279 method: The name of the RPC method.
280 request_serializer: Optional :term:`serializer` for serializing the request
281 message. Request goes unserialized in case None is passed.
282 response_deserializer: Optional :term:`deserializer` for deserializing the
283 response message. Response goes undeserialized in case None
287 A UnaryUnaryMultiCallable value for the named unary-unary method.
294 request_serializer: Optional[SerializingFunction] = None,
295 response_deserializer: Optional[DeserializingFunction] = None
296 ) -> UnaryStreamMultiCallable:
297 """Creates a UnaryStreamMultiCallable for a unary-stream method.
300 method: The name of the RPC method.
301 request_serializer: Optional :term:`serializer` for serializing the request
302 message. Request goes unserialized in case None is passed.
303 response_deserializer: Optional :term:`deserializer` for deserializing the
304 response message. Response goes undeserialized in case None
308 A UnarySteramMultiCallable value for the named unary-stream method.
315 request_serializer: Optional[SerializingFunction] = None,
316 response_deserializer: Optional[DeserializingFunction] = None
317 ) -> StreamUnaryMultiCallable:
318 """Creates a StreamUnaryMultiCallable for a stream-unary method.
321 method: The name of the RPC method.
322 request_serializer: Optional :term:`serializer` for serializing the request
323 message. Request goes unserialized in case None is passed.
324 response_deserializer: Optional :term:`deserializer` for deserializing the
325 response message. Response goes undeserialized in case None
329 A StreamUnaryMultiCallable value for the named stream-unary method.
336 request_serializer: Optional[SerializingFunction] = None,
337 response_deserializer: Optional[DeserializingFunction] = None
338 ) -> StreamStreamMultiCallable:
339 """Creates a StreamStreamMultiCallable for a stream-stream method.
342 method: The name of the RPC method.
343 request_serializer: Optional :term:`serializer` for serializing the request
344 message. Request goes unserialized in case None is passed.
345 response_deserializer: Optional :term:`deserializer` for deserializing the
346 response message. Response goes undeserialized in case None
350 A StreamStreamMultiCallable value for the named stream-stream method.