investify-utils 2.0.0a9__py3-none-any.whl → 2.0.0a10__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- investify_utils/kafka/__init__.py +2 -10
- {investify_utils-2.0.0a9.dist-info → investify_utils-2.0.0a10.dist-info}/METADATA +1 -1
- {investify_utils-2.0.0a9.dist-info → investify_utils-2.0.0a10.dist-info}/RECORD +5 -6
- investify_utils/kafka/async_producer.py +0 -152
- {investify_utils-2.0.0a9.dist-info → investify_utils-2.0.0a10.dist-info}/WHEEL +0 -0
- {investify_utils-2.0.0a9.dist-info → investify_utils-2.0.0a10.dist-info}/entry_points.txt +0 -0
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
|
-
Kafka Avro producer and consumer clients.
|
|
2
|
+
Kafka Avro producer and consumer clients (sync only).
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Usage:
|
|
5
5
|
from investify_utils.kafka import AvroProducer, AvroConsumer
|
|
6
|
-
|
|
7
|
-
Async producer (for LangGraph, FastAPI):
|
|
8
|
-
from investify_utils.kafka import AsyncAvroProducer
|
|
9
6
|
"""
|
|
10
7
|
|
|
11
8
|
|
|
@@ -23,10 +20,6 @@ def __getattr__(name: str):
|
|
|
23
20
|
from investify_utils.kafka.sync_consumer import OffsetTracker
|
|
24
21
|
|
|
25
22
|
return OffsetTracker
|
|
26
|
-
if name == "AsyncAvroProducer":
|
|
27
|
-
from investify_utils.kafka.async_producer import AsyncAvroProducer
|
|
28
|
-
|
|
29
|
-
return AsyncAvroProducer
|
|
30
23
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
31
24
|
|
|
32
25
|
|
|
@@ -34,5 +27,4 @@ __all__ = [
|
|
|
34
27
|
"AvroProducer",
|
|
35
28
|
"AvroConsumer",
|
|
36
29
|
"OffsetTracker",
|
|
37
|
-
"AsyncAvroProducer",
|
|
38
30
|
]
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
investify_utils-2.0.
|
|
2
|
-
investify_utils-2.0.
|
|
3
|
-
investify_utils-2.0.
|
|
1
|
+
investify_utils-2.0.0a10.dist-info/METADATA,sha256=yk0dpTvvrU-C535Jy4yjDXCIpiWZdf94W1jtyw2TRkM,3676
|
|
2
|
+
investify_utils-2.0.0a10.dist-info/WHEEL,sha256=tsUv_t7BDeJeRHaSrczbGeuK-TtDpGsWi_JfpzD255I,90
|
|
3
|
+
investify_utils-2.0.0a10.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
|
|
4
4
|
investify_utils/__init__.py,sha256=sES5ddhmaCRi79i6oax6MhB7H5nJg5QjuG6VV1VI2mc,918
|
|
5
5
|
investify_utils/helpers.py,sha256=1l7nv-P8m-vHQGhjTAJMi-pkvQb8OPzGDIn1KQ499dE,4246
|
|
6
|
-
investify_utils/kafka/__init__.py,sha256=
|
|
7
|
-
investify_utils/kafka/async_producer.py,sha256=HtQ5SaL5ShJf7RetO5zItfg9EBFcPj1y18i_V0p-vFg,4752
|
|
6
|
+
investify_utils/kafka/__init__.py,sha256=QAo0koEnQv939_6yys6z4D1cPlXiAjv-m15d1987Yl0,777
|
|
8
7
|
investify_utils/kafka/sync_consumer.py,sha256=NJu9tQ5MrqL7-0Cvtt9Gmq-Qro_O4VVFP85qIKMf_ZM,6305
|
|
9
8
|
investify_utils/kafka/sync_producer.py,sha256=9EyhKZNCgmBgZw50gwSfSRUTnfPnCq520Mh9MHnGVlI,4498
|
|
10
9
|
investify_utils/logging.py,sha256=SyqlpyRd0o_FuO29eB7-5-Fo0mfLRVZP_YHjXcQgjiE,2223
|
|
@@ -13,4 +12,4 @@ investify_utils/postgres/async_client.py,sha256=M3F7-AsBJ43WWhfknnvTK9BeiYAyO0R6
|
|
|
13
12
|
investify_utils/postgres/sync_client.py,sha256=1mozgrNGUUKCR2ETAr9G9dzvW8uG_TmSqcbA63tRpM8,6507
|
|
14
13
|
investify_utils/s3/__init__.py,sha256=0YX-efJTP38Q5XMCyr7u-fXMjCJXkAR7dG817quTns8,399
|
|
15
14
|
investify_utils/s3/sync_client.py,sha256=fj6ejhAu06BUBRe2pnceKaNGhbPM79Xf47geL0DB-i0,6771
|
|
16
|
-
investify_utils-2.0.
|
|
15
|
+
investify_utils-2.0.0a10.dist-info/RECORD,,
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Asynchronous Avro producer using confluent-kafka with asyncio.
|
|
3
|
-
|
|
4
|
-
Features:
|
|
5
|
-
- Non-blocking produce with async/await
|
|
6
|
-
- Background asyncio task for polling
|
|
7
|
-
- Suitable for async frameworks (LangGraph, FastAPI)
|
|
8
|
-
|
|
9
|
-
Usage:
|
|
10
|
-
from investify_utils.kafka import AsyncAvroProducer
|
|
11
|
-
|
|
12
|
-
producer = AsyncAvroProducer(
|
|
13
|
-
topic="my-topic",
|
|
14
|
-
subject="my-topic-value",
|
|
15
|
-
schema_registry_url="http://localhost:8081",
|
|
16
|
-
bootstrap_servers="localhost:9092",
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
# In async context
|
|
20
|
-
await producer.produce(key="key1", value={"field": "value"})
|
|
21
|
-
producer.close()
|
|
22
|
-
"""
|
|
23
|
-
|
|
24
|
-
import asyncio
|
|
25
|
-
import logging
|
|
26
|
-
from typing import Callable
|
|
27
|
-
|
|
28
|
-
from confluent_kafka import KafkaException, SerializingProducer
|
|
29
|
-
from confluent_kafka.schema_registry import SchemaRegistryClient, record_subject_name_strategy
|
|
30
|
-
from confluent_kafka.schema_registry.avro import AvroSerializer
|
|
31
|
-
from confluent_kafka.serialization import StringSerializer
|
|
32
|
-
|
|
33
|
-
logger = logging.getLogger(__name__)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
class AsyncAvroProducer:
|
|
37
|
-
"""
|
|
38
|
-
Asynchronous Avro producer for async frameworks.
|
|
39
|
-
|
|
40
|
-
Args:
|
|
41
|
-
topic: Kafka topic name
|
|
42
|
-
subject: Schema Registry subject name
|
|
43
|
-
schema_registry_url: Schema Registry URL
|
|
44
|
-
bootstrap_servers: Kafka bootstrap servers
|
|
45
|
-
**kwargs: Additional Kafka producer config
|
|
46
|
-
"""
|
|
47
|
-
|
|
48
|
-
def __init__(
|
|
49
|
-
self,
|
|
50
|
-
topic: str,
|
|
51
|
-
subject: str,
|
|
52
|
-
schema_registry_url: str,
|
|
53
|
-
bootstrap_servers: str,
|
|
54
|
-
**kwargs,
|
|
55
|
-
):
|
|
56
|
-
self.topic = topic
|
|
57
|
-
self._schema_registry_url = schema_registry_url
|
|
58
|
-
self._bootstrap_servers = bootstrap_servers
|
|
59
|
-
self._subject = subject
|
|
60
|
-
self._kwargs = kwargs
|
|
61
|
-
self._producer: SerializingProducer | None = None
|
|
62
|
-
self._poll_task: asyncio.Task | None = None
|
|
63
|
-
|
|
64
|
-
@property
|
|
65
|
-
def producer(self) -> SerializingProducer:
|
|
66
|
-
"""Lazy producer initialization."""
|
|
67
|
-
if self._producer is None:
|
|
68
|
-
schema_registry_client = SchemaRegistryClient({"url": self._schema_registry_url})
|
|
69
|
-
registered_schema = schema_registry_client.get_latest_version(self._subject)
|
|
70
|
-
schema_str = registered_schema.schema.schema_str
|
|
71
|
-
|
|
72
|
-
avro_serializer = AvroSerializer(
|
|
73
|
-
schema_registry_client,
|
|
74
|
-
schema_str,
|
|
75
|
-
conf={
|
|
76
|
-
"auto.register.schemas": False,
|
|
77
|
-
"subject.name.strategy": record_subject_name_strategy,
|
|
78
|
-
},
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
producer_config = {
|
|
82
|
-
"bootstrap.servers": self._bootstrap_servers,
|
|
83
|
-
"key.serializer": StringSerializer("utf_8"),
|
|
84
|
-
"value.serializer": avro_serializer,
|
|
85
|
-
**self._kwargs,
|
|
86
|
-
}
|
|
87
|
-
self._producer = SerializingProducer(producer_config)
|
|
88
|
-
|
|
89
|
-
# Start background polling task
|
|
90
|
-
self._poll_task = asyncio.create_task(self._poll_loop())
|
|
91
|
-
|
|
92
|
-
return self._producer
|
|
93
|
-
|
|
94
|
-
async def _poll_loop(self) -> None:
|
|
95
|
-
"""Background task for polling delivery callbacks."""
|
|
96
|
-
while True:
|
|
97
|
-
self._producer.poll(0.1)
|
|
98
|
-
await asyncio.sleep(0.1)
|
|
99
|
-
|
|
100
|
-
async def produce(
|
|
101
|
-
self,
|
|
102
|
-
value: dict,
|
|
103
|
-
key: str | None = None,
|
|
104
|
-
on_delivery: Callable | None = None,
|
|
105
|
-
) -> asyncio.Future:
|
|
106
|
-
"""
|
|
107
|
-
Produce a message asynchronously.
|
|
108
|
-
|
|
109
|
-
Args:
|
|
110
|
-
value: Message value (dict matching Avro schema)
|
|
111
|
-
key: Optional message key
|
|
112
|
-
on_delivery: Optional callback(err, msg) for delivery confirmation
|
|
113
|
-
|
|
114
|
-
Returns:
|
|
115
|
-
Future that resolves to the delivered message
|
|
116
|
-
"""
|
|
117
|
-
loop = asyncio.get_running_loop()
|
|
118
|
-
result = loop.create_future()
|
|
119
|
-
|
|
120
|
-
def ack(err, msg):
|
|
121
|
-
if err:
|
|
122
|
-
loop.call_soon_threadsafe(result.set_exception, KafkaException(err))
|
|
123
|
-
else:
|
|
124
|
-
loop.call_soon_threadsafe(result.set_result, msg)
|
|
125
|
-
if on_delivery:
|
|
126
|
-
loop.call_soon_threadsafe(on_delivery, err, msg)
|
|
127
|
-
|
|
128
|
-
self.producer.produce(self.topic, key=key, value=value, on_delivery=ack)
|
|
129
|
-
return await result
|
|
130
|
-
|
|
131
|
-
def flush(self, timeout: float = 10.0) -> int:
|
|
132
|
-
"""
|
|
133
|
-
Wait for all messages to be delivered.
|
|
134
|
-
|
|
135
|
-
Args:
|
|
136
|
-
timeout: Maximum time to wait in seconds
|
|
137
|
-
|
|
138
|
-
Returns:
|
|
139
|
-
Number of messages still in queue
|
|
140
|
-
"""
|
|
141
|
-
if self._producer:
|
|
142
|
-
return self._producer.flush(timeout)
|
|
143
|
-
return 0
|
|
144
|
-
|
|
145
|
-
def close(self) -> None:
|
|
146
|
-
"""Cancel polling task and flush pending messages."""
|
|
147
|
-
if self._poll_task:
|
|
148
|
-
self._poll_task.cancel()
|
|
149
|
-
self._poll_task = None
|
|
150
|
-
if self._producer:
|
|
151
|
-
self._producer.flush()
|
|
152
|
-
self._producer = None
|
|
File without changes
|
|
File without changes
|