|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import time |
| 4 | + |
| 5 | +import ydb |
| 6 | + |
| 7 | + |
| 8 | +async def connect(): |
| 9 | + db = ydb.aio.Driver(connection_string="grpc://localhost:2135?database=/local", credentials=ydb.credentials.AnonymousCredentials()) |
| 10 | + reader = ydb.TopicClientAsyncIO(db).topic_reader("/local/topic", consumer="consumer") |
| 11 | + |
| 12 | + |
| 13 | +async def create_reader_and_close_with_context_manager(db: ydb.aio.Driver): |
| 14 | + with ydb.TopicClientAsyncIO(db).topic_reader("/database/topic/path", consumer="consumer") as reader: |
| 15 | + async for message in reader.messages(): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +async def print_message_content(reader: ydb.TopicReaderAsyncIO): |
| 20 | + async for message in reader.messages(): |
| 21 | + print("text", message.data.read().decode("utf-8")) |
| 22 | + # await and async_commit need only for sync commit mode - for wait ack from servr |
| 23 | + await reader.commit(message) |
| 24 | + |
| 25 | + |
| 26 | +async def process_messages_batch_explicit_commit(reader: ydb.TopicReaderAsyncIO): |
| 27 | + # Explicit commit example |
| 28 | + async for batch in reader.batches(max_messages=100, timeout=2): |
| 29 | + async with asyncio.TaskGroup() as tg: |
| 30 | + for message in batch.messages: |
| 31 | + tg.create_task(_process(message)) |
| 32 | + |
| 33 | + # wait complete of process all messages from batch be taskgroup context manager |
| 34 | + # and commit complete batch |
| 35 | + await reader.commit(batch) |
| 36 | + |
| 37 | + |
| 38 | +async def process_messages_batch_context_manager_commit(reader: ydb.TopicReaderAsyncIO): |
| 39 | + # Commit with context manager |
| 40 | + async for batch in reader.batches(): |
| 41 | + async with reader.commit_on_exit(batch), asyncio.TaskGroup() as tg: |
| 42 | + for message in batch.messages: |
| 43 | + tg.create_task(_process(message)) |
| 44 | + |
| 45 | + |
| 46 | +async def get_message_with_timeout(reader: ydb.TopicReaderAsyncIO): |
| 47 | + try: |
| 48 | + message = await asyncio.wait_for(reader.receive_message(), timeout=1) |
| 49 | + except TimeoutError: |
| 50 | + print("Have no new messages in a second") |
| 51 | + return |
| 52 | + |
| 53 | + print("mess", message.data) |
| 54 | + |
| 55 | + |
| 56 | +async def get_all_messages_with_small_wait(reader: ydb.TopicReaderAsyncIO): |
| 57 | + async for message in reader.messages(timeout=1): |
| 58 | + await _process(message) |
| 59 | + print("Have no new messages in a second") |
| 60 | + |
| 61 | + |
| 62 | +async def get_a_message_from_external_loop(reader: ydb.TopicReaderAsyncIO): |
| 63 | + for i in range(10): |
| 64 | + try: |
| 65 | + message = await asyncio.wait_for(reader.receive_message(), timeout=1) |
| 66 | + except TimeoutError: |
| 67 | + return |
| 68 | + await _process(message) |
| 69 | + |
| 70 | + |
| 71 | +async def get_one_batch_from_external_loop_async(reader: ydb.TopicReaderAsyncIO): |
| 72 | + for i in range(10): |
| 73 | + try: |
| 74 | + batch = await asyncio.wait_for(reader.receive_batch(), timeout=2) |
| 75 | + except TimeoutError: |
| 76 | + return |
| 77 | + |
| 78 | + for message in batch.messages: |
| 79 | + await _process(message) |
| 80 | + await reader.commit(batch) |
| 81 | + |
| 82 | + |
| 83 | +async def auto_deserialize_message(db: ydb.aio.Driver): |
| 84 | + # async, batch work similar to this |
| 85 | + |
| 86 | + async with ydb.TopicClientAsyncIO(db).topic_reader("/database/topic/path", consumer="asd", deserializer=json.loads) as reader: |
| 87 | + async for message in reader.messages(): |
| 88 | + print(message.data.Name) # message.data replaces by json.loads(message.data) of raw message |
| 89 | + reader.commit(message) |
| 90 | + |
| 91 | + |
| 92 | +async def commit_batch_with_context(reader: ydb.TopicReaderAsyncIO): |
| 93 | + async for batch in reader.batches(): |
| 94 | + async with reader.commit_on_exit(batch): |
| 95 | + for message in batch.messages: |
| 96 | + if not batch.is_alive: |
| 97 | + break |
| 98 | + await _process(message) |
| 99 | + |
| 100 | + |
| 101 | +async def handle_partition_stop(reader: ydb.TopicReaderAsyncIO): |
| 102 | + async for message in reader.messages(): |
| 103 | + time.sleep(1) # some work |
| 104 | + if message.is_alive: |
| 105 | + time.sleep(123) # some other work |
| 106 | + await reader.commit(message) |
| 107 | + |
| 108 | + |
| 109 | +async def handle_partition_stop_batch(reader: ydb.TopicReaderAsyncIO): |
| 110 | + def process_batch(batch): |
| 111 | + for message in batch.messages: |
| 112 | + if not batch.is_alive: |
| 113 | + # no reason work with expired batch |
| 114 | + # go read next - good batch |
| 115 | + return |
| 116 | + await _process(message) |
| 117 | + await reader.commit(batch) |
| 118 | + |
| 119 | + async for batch in reader.batches(): |
| 120 | + process_batch(batch) |
| 121 | + |
| 122 | + |
| 123 | +async def connect_and_read_few_topics(db: ydb.aio.Driver): |
| 124 | + with ydb.TopicClientAsyncIO(db).topic_reader( |
| 125 | + ["/database/topic/path", ydb.TopicSelector("/database/second-topic", partitions=3)]) as reader: |
| 126 | + async for message in reader.messages(): |
| 127 | + await _process(message) |
| 128 | + await reader.commit(message) |
| 129 | + |
| 130 | + |
| 131 | +async def handle_partition_graceful_stop_batch(reader: ydb.TopicReaderAsyncIO): |
| 132 | + # no special handle, but batch will contain less than prefer count messages |
| 133 | + async for batch in reader.batches(): |
| 134 | + await _process(batch) |
| 135 | + reader.commit(batch) |
| 136 | + |
| 137 | + |
| 138 | +async def advanced_commit_notify(db: ydb.aio.Driver): |
| 139 | + def on_commit(event: ydb.TopicReaderEvents.OnCommit) -> None: |
| 140 | + print(event.topic) |
| 141 | + print(event.offset) |
| 142 | + |
| 143 | + async with ydb.TopicClientAsyncIO(db).topic_reader("/local", |
| 144 | + consumer="consumer", |
| 145 | + commit_batch_time=4, |
| 146 | + on_commit=on_commit) as reader: |
| 147 | + async for message in reader.messages(): |
| 148 | + await _process(message) |
| 149 | + await reader.commit(message) |
| 150 | + |
| 151 | + |
| 152 | +async def advanced_read_with_own_progress_storage(db: ydb.TopicReaderAsyncIO): |
| 153 | + async def on_get_partition_start_offset(req: ydb.TopicReaderEvents.OnPartitionGetStartOffsetRequest) -> \ |
| 154 | + ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse: |
| 155 | + # read current progress from database |
| 156 | + resp = ydb.TopicReaderEvents.OnPartitionGetStartOffsetResponse() |
| 157 | + resp.start_offset = 123 |
| 158 | + return resp |
| 159 | + |
| 160 | + async with ydb.TopicClient(db).topic_reader("/local/test", consumer="consumer", |
| 161 | + on_get_partition_start_offset=on_get_partition_start_offset |
| 162 | + ) as reader: |
| 163 | + async for mess in reader.messages(): |
| 164 | + await _process(mess) |
| 165 | + # save progress to own database |
| 166 | + |
| 167 | + # no commit progress to topic service |
| 168 | + # reader.commit(mess) |
| 169 | + |
| 170 | + |
| 171 | +async def _process(msg): |
| 172 | + raise NotImplementedError() |
0 commit comments