Skip to main content

kafka_pubsub

Small, generic wrappers around confluent-kafka for applications that need explicit Kafka delivery and acknowledgement boundaries.

Install

pip install kafka_pubsub

Runtime dependencies (confluent-kafka and python-dotenv) are declared by the package. For local tests use pip install -e '.[test]'.

API and releases

The current hardened API is intended to become the governed stable 1.0.0 boundary; it is materially breaking relative to the historical public 0.2.x series. The checked-in source intentionally reports the development version 1.0.0.dev0, not a public 0.2.x release. Release version calculation, tagged build overlays, Git releases, and separately manual PyPI publication are described in RELEASE.md.

Consuming safely

Subclass Consumer and make process(message) return a Disposition:

from kafka_pubsub.consumer import Consumer, Disposition

class Orders(Consumer):
    def process(self, message):
        # Complete every application side effect first.
        handle(message.value())
        return Disposition.ACK

ACK synchronously commits after process returns; its CommitResult is passed to on_message_success. TERMINAL also synchronously commits, but calls on_message_terminal; use it only after the application has deliberately handled a poison/terminal message (for example, by its own DLQ policy). RETRY does not commit. Exceptions and invalid dispositions do not commit and are sent to on_message_failure. Consumer enable.auto.commit and enable.auto.offset.store are always disabled. A failed synchronous commit is also sent to that hook and then raised from the runner.

Run consumers with a cooperative stop controller:

from kafka_pubsub.multi_consumer_runner import MultiConsumerRunner, StopController

controller = StopController()
# another thread/signal adapter may call controller.request_stop()
MultiConsumerRunner.run([Orders], controller=controller, drain_timeout=30)

A stop request prevents the next message from entering application work. The current synchronous handler is allowed to return, then initialized consumers are closed in finally. drain_timeout is a bounded runner lifetime mechanism; the package does not kill application work or impose a signal policy.

Producing

Producer.produce_and_wait() (and the compatibility spelling produce()) uses a bounded wait, raises ProducerError or ProducerTimeoutError on enqueue, delivery, or timeout failure, and returns DeliveryMetadata(topic, partition, offset) only after broker-confirmed delivery. kafka_produce() has the same semantics. Producers default to acks=all, retries, and idempotence where the installed librdkafka supports it; this is not exactly-once processing.

Topics and configuration

resolve_topic(topic) applies KAFKA_TOPICS_PREFIX to producer and consumer topics identically, exactly once. A topic already starting with the prefix is treated as resolved. kafka_topics_prefix() remains available.

Package settings use snake_case names from KAFKA_CONFIG; pass advanced confluent-kafka/librdkafka options either as dotted keys or in a client_options dictionary. Unknown non-dotted options fail early. A non-empty consumer_group is required, and automatic commit/offset-store settings are rejected to preserve the explicit acknowledgement contract.

Applications remain responsible for domain retry policy, idempotency, side effects, dead-letter handling, persistence, readiness, and process lifecycle.

Release files for kafka-pubsub 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kafka-pubsub 1.0.0
File Size Uploaded
kafka_pubsub-1.0.0.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kafka-pubsub 1.0.0
File Interpreter ABI Platform
kafka_pubsub-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.0 kB

Release files / kafka_pubsub-1.0.0.tar.gz

Download URL kafka_pubsub-1.0.0.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7d984230fc0bc03074c8758f0e1edd13b0f72aea0342a9c1e9b811b52afa310c
BLAKE2b-256 checksum
How to use checksums
41e2badc0a1542ed8b04bd155cc5fa23fec6e00076ba47a2cef9d0d82e037d07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.14

Release files / kafka_pubsub-1.0.0-py3-none-any.whl

Download URL kafka_pubsub-1.0.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
283d6482c2bb9c8bd341c9faf2d147ae461d99ca453160ea682d9685ca7920ef
BLAKE2b-256 checksum
How to use checksums
f5d971c9fef315b3bfc429e1cd8c27854e7e513eb9d9e986d0e3440375974686
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page