@@ -145,7 +145,7 @@ def close(self):
145
145
SupportedDriverType = Union [ydb .Driver , ydb .aio .Driver ]
146
146
147
147
148
- class AbstractGrpcWrapperAsyncIO (IGrpcWrapperAsyncIO , abc . ABC ):
148
+ class GrpcWrapperAsyncIO (IGrpcWrapperAsyncIO ):
149
149
from_client_grpc : asyncio .Queue
150
150
from_server_grpc : AsyncIterator
151
151
convert_server_grpc_to_wrapper : Callable [[Any ], Any ]
@@ -163,6 +163,13 @@ def __init__(self, convert_server_grpc_to_wrapper):
163
163
def __del__ (self ):
164
164
self ._clean_executor (wait = False )
165
165
166
+ async def start (self , driver : SupportedDriverType , stub , method ):
167
+ if asyncio .iscoroutinefunction (driver .__call__ ):
168
+ await self ._start_asyncio_driver (driver , stub , method )
169
+ else :
170
+ await self ._start_sync_driver (driver , stub , method )
171
+ self ._connection_state = "started"
172
+
166
173
def close (self ):
167
174
self .from_client_grpc .put_nowait (_stop_grpc_connection_marker )
168
175
if self ._stream_call :
@@ -174,35 +181,6 @@ def _clean_executor(self, wait: bool):
174
181
if self ._wait_executor :
175
182
self ._wait_executor .shutdown (wait )
176
183
177
- async def receive (self ) -> Any :
178
- # todo handle grpc exceptions and convert it to internal exceptions
179
- try :
180
- grpc_message = await self .from_server_grpc .__anext__ ()
181
- except (grpc .RpcError , grpc .aio .AioRpcError ) as e :
182
- raise connection ._rpc_error_handler (self ._connection_state , e )
183
-
184
- issues ._process_response (grpc_message )
185
-
186
- if self ._connection_state != "has_received_messages" :
187
- self ._connection_state = "has_received_messages"
188
-
189
- # print("rekby, grpc, received", grpc_message)
190
- return self .convert_server_grpc_to_wrapper (grpc_message )
191
-
192
- def write (self , wrap_message : IToProto ):
193
- grpc_message = wrap_message .to_proto ()
194
- # print("rekby, grpc, send", grpc_message)
195
- self .from_client_grpc .put_nowait (grpc_message )
196
-
197
-
198
- class GrpcWrapperStreamStreamAsyncIO (AbstractGrpcWrapperAsyncIO ):
199
- async def start (self , driver : SupportedDriverType , stub , method ):
200
- if asyncio .iscoroutinefunction (driver .__call__ ):
201
- await self ._start_asyncio_driver (driver , stub , method )
202
- else :
203
- await self ._start_sync_driver (driver , stub , method )
204
- self ._connection_state = "started"
205
-
206
184
async def _start_asyncio_driver (self , driver : ydb .aio .Driver , stub , method ):
207
185
requests_iterator = QueueToIteratorAsyncIO (self .from_client_grpc )
208
186
stream_call = await driver (
@@ -221,30 +199,25 @@ async def _start_sync_driver(self, driver: ydb.Driver, stub, method):
221
199
self ._stream_call = stream_call
222
200
self .from_server_grpc = SyncToAsyncIterator (stream_call .__iter__ (), self ._wait_executor )
223
201
202
+ async def receive (self ) -> Any :
203
+ # todo handle grpc exceptions and convert it to internal exceptions
204
+ try :
205
+ grpc_message = await self .from_server_grpc .__anext__ ()
206
+ except (grpc .RpcError , grpc .aio .AioRpcError ) as e :
207
+ raise connection ._rpc_error_handler (self ._connection_state , e )
224
208
225
- class GrpcWrapperUnaryStreamAsyncIO (AbstractGrpcWrapperAsyncIO ):
226
- async def start (self , driver : SupportedDriverType , request , stub , method ):
227
- if asyncio .iscoroutinefunction (driver .__call__ ):
228
- await self ._start_asyncio_driver (driver , request , stub , method )
229
- else :
230
- await self ._start_sync_driver (driver , request , stub , method )
231
- self ._connection_state = "started"
209
+ issues ._process_response (grpc_message )
232
210
233
- async def _start_asyncio_driver (self , driver : ydb .aio .Driver , request , stub , method ):
234
- stream_call = await driver (
235
- request ,
236
- stub ,
237
- method ,
238
- )
239
- self ._stream_call = stream_call
240
- self .from_server_grpc = stream_call .__aiter__ ()
211
+ if self ._connection_state != "has_received_messages" :
212
+ self ._connection_state = "has_received_messages"
241
213
242
- async def _start_sync_driver ( self , driver : ydb . Driver , request , stub , method ):
243
- self ._wait_executor = concurrent . futures . ThreadPoolExecutor ( max_workers = 1 )
214
+ # print("rekby, grpc, received", grpc_message)
215
+ return self .convert_server_grpc_to_wrapper ( grpc_message )
244
216
245
- stream_call = await to_thread (driver , request , stub , method , executor = self ._wait_executor )
246
- self ._stream_call = stream_call
247
- self .from_server_grpc = SyncToAsyncIterator (stream_call .__iter__ (), self ._wait_executor )
217
+ def write (self , wrap_message : IToProto ):
218
+ grpc_message = wrap_message .to_proto ()
219
+ # print("rekby, grpc, send", grpc_message)
220
+ self .from_client_grpc .put_nowait (grpc_message )
248
221
249
222
250
223
@dataclass (init = False )
@@ -283,19 +256,6 @@ def issue_to_str(cls, issue: ydb_issue_message_pb2.IssueMessage):
283
256
return res
284
257
285
258
286
- ResultType = typing .TypeVar ("ResultType" , bound = IFromProtoWithProtoType )
287
-
288
-
289
- def create_result_wrapper (
290
- result_type : typing .Type [ResultType ],
291
- ) -> typing .Callable [[typing .Any , typing .Any , typing .Any ], ResultType ]:
292
- def wrapper (rpc_state , response_pb , driver = None ):
293
- # issues._process_response(response_pb.operation)
294
- return result_type .from_proto (response_pb )
295
-
296
- return wrapper
297
-
298
-
299
259
def callback_from_asyncio (callback : Union [Callable , Coroutine ]) -> [asyncio .Future , asyncio .Task ]:
300
260
loop = asyncio .get_running_loop ()
301
261
0 commit comments