Skip to main content

Typed Python Kafka Manager - easy publish/listen to kafka topics embraced with strong types

Project description

Kavari: easy, automated Kafka publish/subscription with strong types

This tool is to make usage of kafka super easy and safe, utilizing best practices and power given by confluent_kafka

Publishing message

Create a message type, that defines the payload (our strong typed message format)

class TestKafkaMessage(KafkaMessage):
    topic = "test_topic"

    def __init__(self, payload: str):
        super().__init__()
        self.payload: str = payload

    def get_partition_key(self) -> str:
        return "1"

And then publish it on the topic, just by calling:

msg: TestKafkaMessage = TestKafkaMessage("test_message")
kafka_manager.publish_message(msg, lambda msg, ex:  print("Message published"))

Easy? I hope so! Now let's consume this message

Consuming message

Define the handler class

@kafka_message_handler(message_cls=TestKafkaMessage)
class TestKafkaMessageConsumer(KafkaMessageConsumer):

    def __init__(self):
        self.received_message: str | None = None

    def handle(self, message_data: str) -> None:
        self.received_message = message_data

That's (almost) it! Once consumer become available via provider (any DI for example) each message is handled out of the box

I hope you like the concept!

To achieve full power of this lib, you need to configure it

Configuration

The example one, compatible with DI container:

from kavari import kavari_create, FibonacciRetryPolicy, KafkaManager


class Container(DeclarativeContainer):
    kafka_manager: Singleton[KafkaManager] = Singleton(
        lambda: kavari_create(
            bootstrap_servers="bootstrap_location:2973",
            group_id="unique_group_identifier",
            publishing_retry_policy=FibonacciRetryPolicy(max_attempts=10),
            logger=logger,
            auto_commit=False,
            auto_offset_reset="earliest"
        )
    )

Then in the bootstrap of the project add (here, with the FastAPI):

@asynccontextmanager
async def lifespan(app: FastAPI):
    # this part is called on application start
    container.logger().info("Initiating startup & background jobs")
    # consumer provider is called to get particular type of the consumer, making 
    # the autoresolve feature working out of the box
    container.kafka_manager().set_consumer_provider(container.resolve) 
    container.kafka_manager().start_consumer_loop()
    yield
    # this part is called when application is tearing down
    container.logger().info("Stopping background jobs")
    container.kafka_manager().stop_consumer_loop()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kavari-0.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kavari-0.1.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file kavari-0.1.0.tar.gz.

File metadata

  • Download URL: kavari-0.1.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for kavari-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b09aef748db246c3e7acffada2252077fa56f72dde68e8b3d6860864d1f3ce47
MD5 be222c781abecb8c72ceb67e2baa5cb6
BLAKE2b-256 8585e8ed8bacabc133f6d5f1ce19635d2b0f290f2083d42f480bff164984d5cd

See more details on using hashes here.

File details

Details for the file kavari-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kavari-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for kavari-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 709dc69424f119b8d4652aab8a25f6c1d9108df36e7a0caf7f959e6c415447e7
MD5 bfd582935efda5a656d96619b40ab552
BLAKE2b-256 0b02e57ab09ac24f85eb4ede16e50317c00b9e9e52e6949f2603d3134daed864

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page