1
+ import asyncio
2
+ import concurrent .futures
3
+ import typing
4
+ from typing import List , Union , Iterable , Optional , Coroutine
5
+
6
+ from ydb ._grpc .grpcwrapper .common_utils import SupportedDriverType
7
+ from ydb ._topic_common .common import _get_shared_event_loop
8
+ from ydb ._topic_reader .datatypes import PublicMessage , PublicBatch , ICommittable
9
+ from ydb ._topic_reader .topic_reader import (
10
+ PublicReaderSettings ,
11
+ SessionStat ,
12
+ CommitResult ,
13
+ )
14
+ from ydb ._topic_reader .topic_reader_asyncio import (
15
+ PublicAsyncIOReader ,
16
+ TopicReaderClosedError ,
17
+ )
18
+
1
19
2
20
class TopicReaderSync :
21
+ _loop : asyncio .AbstractEventLoop
22
+ _async_reader : PublicAsyncIOReader
23
+ _closed : bool
24
+
25
+ def __init__ (
26
+ self ,
27
+ driver : SupportedDriverType ,
28
+ settings : PublicReaderSettings ,
29
+ * ,
30
+ eventloop : Optional [asyncio .AbstractEventLoop ] = None ,
31
+ ):
32
+ self ._closed = False
33
+
34
+ if eventloop :
35
+ self ._loop = eventloop
36
+ else :
37
+ self ._loop = _get_shared_event_loop ()
38
+
39
+ async def create_reader ():
40
+ return PublicAsyncIOReader (driver , settings )
41
+
42
+ self ._async_reader = asyncio .run_coroutine_threadsafe (
43
+ create_reader (), self ._loop
44
+ ).result ()
45
+
46
+ def __del__ (self ):
47
+ self .close ()
48
+
49
+ def _call (self , coro ):
50
+ if self ._closed :
51
+ raise TopicReaderClosedError ()
52
+
53
+ return asyncio .run_coroutine_threadsafe (coro , self ._loop )
54
+
55
+ def _call_sync (self , coro : Coroutine , timeout ):
56
+ f = self ._call (coro )
57
+ try :
58
+ return f .result (timeout )
59
+ except TimeoutError :
60
+ f .cancel ()
61
+ raise
62
+
3
63
def async_sessions_stat (self ) -> concurrent .futures .Future :
4
64
"""
5
65
Receive stat from the server, return feature.
6
66
"""
7
67
raise NotImplementedError ()
8
68
9
- async def sessions_stat (self ) -> List [" SessionStat" ]:
69
+ async def sessions_stat (self ) -> List [SessionStat ]:
10
70
"""
11
71
Receive stat from the server
12
72
@@ -67,8 +127,8 @@ def batches(
67
127
def receive_batch (
68
128
self ,
69
129
* ,
70
- max_messages : Union [int , None ] = None ,
71
- max_bytes : Union [int , None ],
130
+ max_messages : typing . Union [int , None ] = None ,
131
+ max_bytes : typing . Union [int , None ] = None ,
72
132
timeout : Union [float , None ] = None ,
73
133
) -> Union [PublicBatch , None ]:
74
134
"""
@@ -78,7 +138,12 @@ def receive_batch(
78
138
if no new message in timeout seconds (default - infinite): raise TimeoutError()
79
139
if timeout <= 0 - it will fast non block method, get messages from internal buffer only.
80
140
"""
81
- raise NotImplementedError ()
141
+ return self ._call_sync (
142
+ self ._async_reader .receive_batch (
143
+ max_messages = max_messages , max_bytes = max_bytes
144
+ ),
145
+ timeout ,
146
+ )
82
147
83
148
def commit (self , mess : ICommittable ):
84
149
"""
@@ -87,11 +152,11 @@ def commit(self, mess: ICommittable):
87
152
For the method no way check the commit result
88
153
(for example if lost connection - commits will not re-send and committed messages will receive again)
89
154
"""
90
- raise NotImplementedError ( )
155
+ self . _call_sync ( self . _async_reader . commit ( mess ), None )
91
156
92
157
def commit_with_ack (
93
158
self , mess : ICommittable
94
- ) -> Union [" CommitResult" , List [" CommitResult" ]]:
159
+ ) -> Union [CommitResult , List [CommitResult ]]:
95
160
"""
96
161
write commit message to a buffer and wait ack from the server.
97
162
@@ -101,7 +166,7 @@ def commit_with_ack(
101
166
102
167
def async_commit_with_ack (
103
168
self , mess : ICommittable
104
- ) -> Union [" CommitResult" , List [" CommitResult" ]]:
169
+ ) -> Union [CommitResult , List [CommitResult ]]:
105
170
"""
106
171
write commit message to a buffer and return Future for wait result.
107
172
"""
@@ -120,4 +185,8 @@ def flush(self):
120
185
raise NotImplementedError ()
121
186
122
187
def close (self ):
123
- raise NotImplementedError ()
188
+ if self ._closed :
189
+ return
190
+ self ._closed = True
191
+
192
+ self ._call_sync (self ._async_reader .close ())
0 commit comments