1
- import asyncio
2
1
import argparse
3
2
import logging
4
3
import ydb
5
4
6
5
7
- async def connect (endpoint : str , database : str ) -> ydb . aio .Driver :
6
+ def connect (endpoint : str , database : str ) -> ydb .Driver :
8
7
config = ydb .DriverConfig (endpoint = endpoint , database = database )
9
8
config .credentials = ydb .credentials_from_env_variables ()
10
- driver = ydb .aio . Driver (config )
11
- await driver .wait (5 , fail_fast = True )
9
+ driver = ydb .Driver (config )
10
+ driver .wait (5 , fail_fast = True )
12
11
return driver
13
12
14
13
15
- async def create_topic (driver : ydb . aio .Driver , topic : str , consumer : str ):
14
+ def create_topic (driver : ydb .Driver , topic : str , consumer : str ):
16
15
try :
17
- await driver .topic_client .drop_topic (topic )
16
+ driver .topic_client .drop_topic (topic )
18
17
except ydb .SchemeError :
19
18
pass
20
19
21
- await driver .topic_client .create_topic (topic , consumers = [consumer ])
20
+ driver .topic_client .create_topic (topic , consumers = [consumer ])
22
21
23
22
24
- async def write_with_tx_example (driver : ydb . aio .Driver , topic : str , message_count : int = 10 ):
25
- async with ydb . aio .QuerySessionPool (driver ) as session_pool :
23
+ def write_with_tx_example (driver : ydb .Driver , topic : str , message_count : int = 10 ):
24
+ with ydb .QuerySessionPool (driver ) as session_pool :
26
25
27
- async def callee (tx : ydb .aio .QueryTxContext ):
28
- print (f"TX ID: { tx .tx_id } " )
29
- print (f"TX STATE: { tx ._tx_state ._state .value } " )
26
+ def callee (tx : ydb .aio .QueryTxContext ):
30
27
tx_writer : ydb .TopicTxWriterAsyncIO = driver .topic_client .tx_writer (tx , topic )
31
- print (f"TX ID: { tx .tx_id } " )
32
- print (f"TX STATE: { tx ._tx_state ._state .value } " )
33
28
for i in range (message_count ):
34
- result_stream = await tx .execute (query = f"select { i } as res" )
35
- messages = [result_set .rows [0 ]["res" ] async for result_set in result_stream ]
29
+ result_stream = tx .execute (query = f"select { i } as res" )
30
+ messages = [result_set .rows [0 ]["res" ] for result_set in result_stream ]
36
31
37
- await tx_writer .write ([ydb .TopicWriterMessage (data = str (message )) for message in messages ])
32
+ tx_writer .write ([ydb .TopicWriterMessage (data = str (message )) for message in messages ])
38
33
39
34
print (f"Messages { messages } were written with tx." )
40
35
41
- await session_pool .retry_tx_async (callee )
36
+ session_pool .retry_tx_sync (callee )
42
37
43
38
44
- async def read_with_tx_example (driver : ydb . aio .Driver , topic : str , consumer : str , message_count : int = 10 ):
45
- async with driver .topic_client .reader (topic , consumer ) as reader :
46
- async with ydb . aio .QuerySessionPool (driver ) as session_pool :
39
+ def read_with_tx_example (driver : ydb .Driver , topic : str , consumer : str , message_count : int = 10 ):
40
+ with driver .topic_client .reader (topic , consumer ) as reader :
41
+ with ydb .QuerySessionPool (driver ) as session_pool :
47
42
for _ in range (message_count ):
48
43
49
- async def callee (tx : ydb .aio .QueryTxContext ):
50
- batch = await reader .receive_batch_with_tx (tx , max_messages = 1 )
51
- print (f"Messages { batch .messages [0 ].data } were read with tx." )
44
+ def callee (tx : ydb .aio .QueryTxContext ):
45
+ batch = reader .receive_batch_with_tx (tx , max_messages = 1 )
46
+ print (f"Messages [ { batch .messages [0 ].data . decode () } ] were read with tx." )
52
47
53
- await session_pool .retry_tx_async (callee )
48
+ session_pool .retry_tx_sync (callee )
54
49
55
50
56
- async def main ():
51
+ def main ():
57
52
parser = argparse .ArgumentParser (
58
53
formatter_class = argparse .RawDescriptionHelpFormatter ,
59
54
description = """YDB topic basic example.\n """ ,
@@ -78,13 +73,13 @@ async def main():
78
73
logger .setLevel (logging .DEBUG )
79
74
logger .addHandler (logging .StreamHandler ())
80
75
81
- driver = await connect (args .endpoint , args .database )
82
- if not args .skip_drop_and_create_topic :
83
- await create_topic (driver , args .path , args .consumer )
76
+ with connect (args .endpoint , args .database ) as driver :
77
+ if not args .skip_drop_and_create_topic :
78
+ create_topic (driver , args .path , args .consumer )
84
79
85
- await write_with_tx_example (driver , args .path )
86
- await read_with_tx_example (driver , args .path , args .consumer )
80
+ write_with_tx_example (driver , args .path )
81
+ read_with_tx_example (driver , args .path , args .consumer )
87
82
88
83
89
84
if __name__ == "__main__" :
90
- asyncio . run ( main () )
85
+ main ()
0 commit comments