Registry / database / pykafka

pykafka

JSON →
library2.8.0pypypiunverified

PyKafka is a full-featured, pure-Python client for Apache Kafka, which optionally uses a C extension (librdkafka) for improved performance. It supports Kafka versions 0.8.2 and newer, providing Pythonic implementations of Kafka producers and consumers. The library aims to offer a similar abstraction level to the JVM Kafka client. PyKafka is actively maintained, with the current stable version being 2.8.0, and typically releases updates as needed.

pip install pykafka
INSTALL
IMPORT
SIG · PYKAFKA
P
pykafka
databasepythonv2.8.0
Install
2.9s avg
Import
433ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.282s · 22.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.9s · import 0.237s · 23MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KafkaClient
from pykafka import KafkaClient
from pykafka import KafkaClient

This quickstart demonstrates how to initialize a PyKafka client, produce a few messages to a topic, and then consume them using a `SimpleConsumer`. It's configured to connect to a local Kafka instance and resets the consumer offset to the earliest available for demonstration purposes. Ensure `KAFKA_HOSTS`, `KAFKA_TOPIC`, and `KAFKA_CONSUMER_GROUP` environment variables are set or default to `localhost:9092` and `test-topic`, `my-consumer-group` respectively.

import os import time from pykafka import KafkaClient from pykafka.common import OffsetType KAFKA_HOSTS = os.environ.get('KAFKA_HOSTS', 'localhost:9092') TOPIC_NAME = os.environ.get('KAFKA_TOPIC', 'test-topic').encode('utf-8') CONSUMER_GROUP = os.environ.get('KAFKA_CONSUMER_GROUP', 'my-consumer-group').encode('utf-8') # 1. Connect to Kafka client = KafkaClient(hosts=KAFKA_HOSTS) topic = client.topics[TOPIC_NAME] # 2. Produce a message with topic.get_producer() as producer: print(f"Producing message to {TOPIC_NAME.decode()}...") for i in range(5): message_value = f"test message {i}".encode('utf-8') producer.produce(message_value) print(f"Sent: {message_value.decode()}") print("Finished producing messages.") # Give Kafka a moment to process time.sleep(2) # 3. Consume messages print(f"\nConsuming messages from {TOPIC_NAME.decode()} (group: {CONSUMER_GROUP.decode()})...") consumer = topic.get_simple_consumer( consumer_group=CONSUMER_GROUP, auto_commit_enable=True, auto_offset_reset=OffsetType.EARLIEST, # Start from the beginning if no offset is committed reset_offset_on_start=True # Force reset on start, useful for quickstart ) for i, message in enumerate(consumer): if message is not None: print(f"Received: Offset={message.offset}, Value={message.value.decode()}") if i >= 4: # Consume the 5 messages we sent break consumer.stop() print("Finished consuming messages.")
Debug
Known issues
breakingIn PyKafka 2.8.0, the `consumer_group` keyword argument for consumer components and the parameter for `TopicDict.__getitem__` (when accessing topics) changed their expected type from `bytes` to `str`. If you were passing byte strings, this will now raise a `TypeError`.
fix
Ensure `consumer_group` and topic names are passed as `str` instead of `bytes`. Convert existing byte strings using `.decode('utf-8')` if necessary, or ensure they are defined as `str`.
affects: >=2.8.0
gotchaWhen using `Producer` with `delivery_reports=True`, it's critical to regularly drain the delivery report queue. Failing to do so can lead to unbounded memory growth, as reports are stored in memory until consumed.
fix
Implement a loop to regularly call `producer.get_delivery_report()` and process the returned reports to prevent memory leaks. This is often done in a separate thread or an asynchronous callback.
affects: All
gotchaThe behavior of `auto_offset_reset` and `reset_offset_on_start` in consumers can be counter-intuitive. `reset_offset_on_start=True` will *always* reset the offset based on `auto_offset_reset` for the first fetch, even if committed offsets exist. If `False`, it will use committed offsets if available.
fix
Carefully review the `Consumer Patterns` documentation regarding `auto_offset_reset` (`OffsetType.EARLIEST` or `OffsetType.LATEST`) and `reset_offset_on_start` to ensure the consumer starts at the intended offset, especially in production environments to avoid message loss or reprocessing.
affects: All
gotchaWhile PyKafka is a pure-Python client, its high-performance C extension (librdkafka) is highly recommended for production use. Without it, performance can be significantly lower, and certain features might be less robust. Installation of `librdkafka` requires system-level dependencies.
fix
Install `librdkafka` development packages on your system (e.g., `librdkafka-dev` on Debian/Ubuntu, `librdkafka-devel` on RHEL/CentOS) and then install pykafka using `RDKAFKA_INSTALL=system pip install pykafka`.
affects: All
Upgrade
Version history
2.8.0latest on PyPI · released Sep 24, 2018
Audit
Dependencies
librdkafkaoptionalOptional C extension for high-performance producer and consumer operations. Requires system-level installation of librdkafka headers and shared libraries.
lz4optionalRequired for LZ4 compression support.
xxhashoptionalRequired for XXHash algorithm support, used in some Kafka protocol features.
Agent activity
28 hits · last 30 days
node
26
Resources
pykafka — pip install pykafka · libregistry