@@ -75,7 +75,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
75
75
76
76
def __del__ (self ):
77
77
if not self ._closed :
78
- self ._loop .create_task (self .close (), name = "close reader" )
78
+ self ._loop .create_task (self .close (flush = False ), name = "close reader" )
79
79
80
80
async def receive_batch (
81
81
self ,
@@ -120,12 +120,12 @@ async def commit_with_ack(
120
120
waiter = self ._reconnector .commit (batch )
121
121
await waiter .future
122
122
123
- async def close (self ):
123
+ async def close (self , flush : bool = True ):
124
124
if self ._closed :
125
125
raise TopicReaderClosedError ()
126
126
127
127
self ._closed = True
128
- await self ._reconnector .close ()
128
+ await self ._reconnector .close (flush )
129
129
130
130
131
131
class ReaderReconnector :
@@ -199,14 +199,20 @@ def commit(
199
199
) -> datatypes .PartitionSession .CommitAckWaiter :
200
200
return self ._stream_reader .commit (batch )
201
201
202
- async def close (self ):
202
+ async def close (self , flush : bool ):
203
203
if self ._stream_reader :
204
+ if flush :
205
+ await self .flush ()
204
206
await self ._stream_reader .close ()
205
207
for task in self ._background_tasks :
206
208
task .cancel ()
207
209
208
210
await asyncio .wait (self ._background_tasks )
209
211
212
+ async def flush (self ):
213
+ if self ._stream_reader :
214
+ await self ._stream_reader .flush ()
215
+
210
216
def _set_first_error (self , err : issues .Error ):
211
217
try :
212
218
self ._first_error .set_result (err )
@@ -641,6 +647,16 @@ def _get_first_error(self) -> Optional[YdbError]:
641
647
if self ._first_error .done ():
642
648
return self ._first_error .result ()
643
649
650
+ async def flush (self ):
651
+ if self ._closed :
652
+ raise RuntimeError ("Flush on closed Stream" )
653
+
654
+ futures = []
655
+ for session in self ._partition_sessions .values ():
656
+ futures .extend (w .future for w in session ._ack_waiters )
657
+
658
+ await asyncio .gather (* futures )
659
+
644
660
async def close (self ):
645
661
if self ._closed :
646
662
return
0 commit comments