1- import json
21import time
32
43import ydb
@@ -37,14 +36,6 @@ def process_messages_batch_explicit_commit(reader: ydb.TopicReader):
3736 reader .commit (batch )
3837
3938
40- def process_messages_batch_context_manager_commit (reader : ydb .TopicReader ):
41- while True :
42- batch = reader .receive_batch ()
43- with reader .commit_on_exit (batch ):
44- for message in batch .messages :
45- _process (message )
46-
47-
4839def get_message_with_timeout (reader : ydb .TopicReader ):
4940 try :
5041 message = reader .receive_message (timeout = 1 )
@@ -85,33 +76,19 @@ def get_one_batch_from_external_loop(reader: ydb.TopicReader):
8576 reader .commit (batch )
8677
8778
88- def auto_deserialize_message (db : ydb .Driver ):
89- # async, batch work similar to this
90-
91- reader = db .topic_client .reader (
92- "/database/topic/path" , consumer = "asd" , deserializer = json .loads
93- )
94- while True :
95- message = reader .receive_message ()
96- print (
97- message .data .Name
98- ) # message.data replaces by json.loads(message.data) of raw message
99- reader .commit (message )
100-
101-
10279def handle_partition_stop (reader : ydb .TopicReader ):
10380 while True :
10481 message = reader .receive_message ()
10582 time .sleep (123 ) # some work
106- if message .is_alive :
83+ if message .alive :
10784 time .sleep (1 ) # some other work
10885 reader .commit (message )
10986
11087
11188def handle_partition_stop_batch (reader : ydb .TopicReader ):
11289 def process_batch (batch ):
11390 for message in batch .messages :
114- if not batch .is_alive :
91+ if not batch .alive :
11592 # no reason work with expired batch
11693 # go read next - good batch
11794 return
@@ -123,19 +100,6 @@ def process_batch(batch):
123100 process_batch (batch )
124101
125102
126- def connect_and_read_few_topics (db : ydb .Driver ):
127- with db .topic_client .reader (
128- [
129- "/database/topic/path" ,
130- ydb .TopicSelector ("/database/second-topic" , partitions = 3 ),
131- ]
132- ) as reader :
133- while True :
134- message = reader .receive_message ()
135- _process (message )
136- reader .commit (message )
137-
138-
139103def handle_partition_graceful_stop_batch (reader : ydb .TopicReader ):
140104 # no special handle, but batch will contain less than prefer count messages
141105 while True :
@@ -144,54 +108,5 @@ def handle_partition_graceful_stop_batch(reader: ydb.TopicReader):
144108 reader .commit (batch )
145109
146110
147- def advanced_commit_notify (db : ydb .Driver ):
148- def on_commit (event : ydb .TopicReaderEvents .OnCommit ) -> None :
149- print (event .topic )
150- print (event .offset )
151-
152- with db .topic_client .reader (
153- "/local" , consumer = "consumer" , commit_batch_time = 4 , on_commit = on_commit
154- ) as reader :
155- while True :
156- message = reader .receive_message ()
157- with reader .commit_on_exit (message ):
158- _process (message )
159-
160-
161- def advanced_read_with_own_progress_storage (db : ydb .TopicReader ):
162- def on_get_partition_start_offset (
163- req : ydb .TopicReaderEvents .OnPartitionGetStartOffsetRequest ,
164- ) -> ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse :
165-
166- # read current progress from database
167- resp = ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse ()
168- resp .start_offset = 123
169- return resp
170-
171- with db .topic_client .reader (
172- "/local/test" ,
173- consumer = "consumer" ,
174- on_get_partition_start_offset = on_get_partition_start_offset ,
175- ) as reader :
176- while True :
177- mess = reader .receive_message ()
178- _process (mess )
179- # save progress to own database
180-
181- # no commit progress to topic service
182- # reader.commit(mess)
183-
184-
185- def get_current_statistics (reader : ydb .TopicReader ):
186- # sync
187- stat = reader .sessions_stat ()
188- print (stat )
189-
190- # with feature
191- f = reader .async_sessions_stat ()
192- stat = f .result ()
193- print (stat )
194-
195-
196111def _process (msg ):
197112 raise NotImplementedError ()
0 commit comments