2626 Codec ,
2727)
2828from .._errors import check_retriable_error
29+ from .. import topic
2930
3031
3132class TopicReaderError (YdbError ):
@@ -61,11 +62,19 @@ class PublicAsyncIOReader:
6162 _loop : asyncio .AbstractEventLoop
6263 _closed : bool
6364 _reconnector : ReaderReconnector
65+ _parent : typing .Any # need for prevent close parent client by GC
6466
65- def __init__ (self , driver : Driver , settings : topic_reader .PublicReaderSettings ):
67+ def __init__ (
68+ self ,
69+ driver : Driver ,
70+ settings : topic_reader .PublicReaderSettings ,
71+ * ,
72+ _parent = None ,
73+ ):
6674 self ._loop = asyncio .get_running_loop ()
6775 self ._closed = False
6876 self ._reconnector = ReaderReconnector (driver , settings )
77+ self ._parent = _parent
6978
7079 async def __aenter__ (self ):
7180 return self
@@ -78,7 +87,7 @@ def __del__(self):
7887 self ._loop .create_task (self .close (flush = False ), name = "close reader" )
7988
8089 async def receive_batch (
81- self ,
90+ self ,
8291 ) -> typing .Union [datatypes .PublicBatch , None ]:
8392 """
8493 Get one messages batch from reader.
@@ -99,7 +108,7 @@ async def receive_message(self) -> typing.Optional[datatypes.PublicMessage]:
99108 return self ._reconnector .receive_message_nowait ()
100109
101110 def commit (
102- self , batch : typing .Union [datatypes .PublicMessage , datatypes .PublicBatch ]
111+ self , batch : typing .Union [datatypes .PublicMessage , datatypes .PublicBatch ]
103112 ):
104113 """
105114 Write commit message to a buffer.
@@ -110,7 +119,7 @@ def commit(
110119 self ._reconnector .commit (batch )
111120
112121 async def commit_with_ack (
113- self , batch : typing .Union [datatypes .PublicMessage , datatypes .PublicBatch ]
122+ self , batch : typing .Union [datatypes .PublicMessage , datatypes .PublicBatch ]
114123 ):
115124 """
116125 write commit message to a buffer and wait ack from the server.
@@ -195,7 +204,7 @@ def receive_message_nowait(self):
195204 return self ._stream_reader .receive_message_nowait ()
196205
197206 def commit (
198- self , batch : datatypes .ICommittable
207+ self , batch : datatypes .ICommittable
199208 ) -> datatypes .PartitionSession .CommitAckWaiter :
200209 return self ._stream_reader .commit (batch )
201210
@@ -254,10 +263,10 @@ class ReaderStream:
254263 _get_token_function : Callable [[], str ]
255264
256265 def __init__ (
257- self ,
258- reader_reconnector_id : int ,
259- settings : topic_reader .PublicReaderSettings ,
260- get_token_function : Optional [Callable [[], str ]] = None ,
266+ self ,
267+ reader_reconnector_id : int ,
268+ settings : topic_reader .PublicReaderSettings ,
269+ get_token_function : Optional [Callable [[], str ]] = None ,
261270 ):
262271 self ._loop = asyncio .get_running_loop ()
263272 self ._id = ReaderStream ._static_id_counter .inc_and_get ()
@@ -286,9 +295,9 @@ def __init__(
286295
287296 @staticmethod
288297 async def create (
289- reader_reconnector_id : int ,
290- driver : SupportedDriverType ,
291- settings : topic_reader .PublicReaderSettings ,
298+ reader_reconnector_id : int ,
299+ driver : SupportedDriverType ,
300+ settings : topic_reader .PublicReaderSettings ,
292301 ) -> "ReaderStream" :
293302 stream = GrpcWrapperAsyncIO (StreamReadMessage .FromServer .from_proto )
294303
@@ -306,7 +315,7 @@ async def create(
306315 return reader
307316
308317 async def _start (
309- self , stream : IGrpcWrapperAsyncIO , init_message : StreamReadMessage .InitRequest
318+ self , stream : IGrpcWrapperAsyncIO , init_message : StreamReadMessage .InitRequest
310319 ):
311320 if self ._started :
312321 raise TopicReaderError ("Double start ReaderStream" )
@@ -372,7 +381,7 @@ def receive_message_nowait(self):
372381 return message
373382
374383 def commit (
375- self , batch : datatypes .ICommittable
384+ self , batch : datatypes .ICommittable
376385 ) -> datatypes .PartitionSession .CommitAckWaiter :
377386 partition_session = batch ._commit_get_partition_session ()
378387
@@ -426,19 +435,19 @@ async def _read_messages_loop(self):
426435 self ._on_read_response (message .server_message )
427436
428437 elif isinstance (
429- message .server_message , StreamReadMessage .CommitOffsetResponse
438+ message .server_message , StreamReadMessage .CommitOffsetResponse
430439 ):
431440 self ._on_commit_response (message .server_message )
432441
433442 elif isinstance (
434- message .server_message ,
435- StreamReadMessage .StartPartitionSessionRequest ,
443+ message .server_message ,
444+ StreamReadMessage .StartPartitionSessionRequest ,
436445 ):
437446 self ._on_start_partition_session (message .server_message )
438447
439448 elif isinstance (
440- message .server_message ,
441- StreamReadMessage .StopPartitionSessionRequest ,
449+ message .server_message ,
450+ StreamReadMessage .StopPartitionSessionRequest ,
442451 ):
443452 self ._on_partition_session_stop (message .server_message )
444453
@@ -470,12 +479,12 @@ async def _update_token(self, token: str):
470479 self ._update_token_event .clear ()
471480
472481 def _on_start_partition_session (
473- self , message : StreamReadMessage .StartPartitionSessionRequest
482+ self , message : StreamReadMessage .StartPartitionSessionRequest
474483 ):
475484 try :
476485 if (
477- message .partition_session .partition_session_id
478- in self ._partition_sessions
486+ message .partition_session .partition_session_id
487+ in self ._partition_sessions
479488 ):
480489 raise TopicReaderError (
481490 "Double start partition session: %s"
@@ -506,7 +515,7 @@ def _on_start_partition_session(
506515 self ._set_first_error (err )
507516
508517 def _on_partition_session_stop (
509- self , message : StreamReadMessage .StopPartitionSessionRequest
518+ self , message : StreamReadMessage .StopPartitionSessionRequest
510519 ):
511520 if message .partition_session_id not in self ._partition_sessions :
512521 # may if receive stop partition with graceful=false after response on stop partition
@@ -554,7 +563,7 @@ def _buffer_release_bytes(self, bytes_size):
554563 )
555564
556565 def _read_response_to_batches (
557- self , message : StreamReadMessage .ReadResponse
566+ self , message : StreamReadMessage .ReadResponse
558567 ) -> typing .List [datatypes .PublicBatch ]:
559568 batches = []
560569
@@ -564,7 +573,7 @@ def _read_response_to_batches(
564573
565574 bytes_per_batch = message .bytes_size // batch_count
566575 additional_bytes_to_last_batch = (
567- message .bytes_size - bytes_per_batch * batch_count
576+ message .bytes_size - bytes_per_batch * batch_count
568577 )
569578
570579 for partition_data in message .partition_data :
0 commit comments