| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import sys |
| 19 |
import asyncio |
| 20 |
if sys.version_info >= (3, 11): |
| 21 |
from asyncio import timeout |
| 22 |
else: |
| 23 |
from async_timeout import timeout |
| 24 |
|
| 25 |
from confluent_kafka.cimpl import Consumer |
| 26 |
from confluent_kafka.error import ConsumeError, KeyDeserializationError, ValueDeserializationError |
| 27 |
from confluent_kafka.serialization import MessageField, SerializationContext |
| 28 |
|
| 29 |
ASYNC_CONSUMER_POLL_INTERVAL_SECONDS: int = 0.2 |
| 30 |
ASYNC_CONSUMER_POLL_INFINITE_TIMEOUT_SECONDS: int = -1 |
| 31 |
|
| 32 |
|
| 33 |
class AsyncConsumer(Consumer): |
| 34 |
def __init__( |
| 35 |
self, |
| 36 |
conf: dict, |
| 37 |
loop: asyncio.AbstractEventLoop = None, |
| 38 |
poll_interval_seconds: int = ASYNC_CONSUMER_POLL_INTERVAL_SECONDS |
| 39 |
): |
| 40 |
super().__init__(conf) |
| 41 |
|
| 42 |
self._loop = loop or asyncio.get_event_loop() |
| 43 |
self._poll_interval = poll_interval_seconds |
| 44 |
|
| 45 |
def __aiter__(self): |
| 46 |
return self |
| 47 |
|
| 48 |
async def __anext__(self): |
| 49 |
return await self.poll(None) |
| 50 |
|
| 51 |
async def poll(self, poll_timeout: int = -1): |
| 52 |
poll_timeout = None if poll_timeout == -1 else poll_timeout |
| 53 |
async with timeout(poll_timeout): |
| 54 |
while True: |
| 55 |
|
| 56 |
msg = super().poll(0) |
| 57 |
if msg is not None: |
| 58 |
return msg |
| 59 |
else: |
| 60 |
await asyncio.sleep(self._poll_interval) |
| 61 |
|
| 62 |
|
| 63 |
class TestAsyncDeserializingConsumer(AsyncConsumer): |
| 64 |
def __init__(self, conf): |
| 65 |
conf_copy = conf.copy() |
| 66 |
self._key_deserializer = conf_copy.pop('key.deserializer', None) |
| 67 |
self._value_deserializer = conf_copy.pop('value.deserializer', None) |
| 68 |
super().__init__(conf_copy) |
| 69 |
|
| 70 |
async def poll(self, poll_timeout=-1): |
| 71 |
msg = await super().poll(poll_timeout) |
| 72 |
|
| 73 |
if msg is None: |
| 74 |
return None |
| 75 |
|
| 76 |
if msg.error() is not None: |
| 77 |
raise ConsumeError(msg.error(), kafka_message=msg) |
| 78 |
|
| 79 |
ctx = SerializationContext(msg.topic(), MessageField.VALUE, msg.headers()) |
| 80 |
value = msg.value() |
| 81 |
if self._value_deserializer is not None: |
| 82 |
try: |
| 83 |
value = await self._value_deserializer(value, ctx) |
| 84 |
except Exception as se: |
| 85 |
raise ValueDeserializationError(exception=se, kafka_message=msg) |
| 86 |
|
| 87 |
key = msg.key() |
| 88 |
ctx.field = MessageField.KEY |
| 89 |
if self._key_deserializer is not None: |
| 90 |
try: |
| 91 |
key = await self._key_deserializer(key, ctx) |
| 92 |
except Exception as se: |
| 93 |
raise KeyDeserializationError(exception=se, kafka_message=msg) |
| 94 |
|
| 95 |
msg.set_key(key) |
| 96 |
msg.set_value(value) |
| 97 |
return msg |
| 98 |
|
| 99 |
def consume(self, num_messages=1, consume_timeout=-1): |
| 100 |
""" |
| 101 |
:py:func:`Consumer.consume` not implemented, use |
| 102 |
:py:func:`DeserializingConsumer.poll` instead |
| 103 |
""" |
| 104 |
|
| 105 |
raise NotImplementedError |