Install & Compatibility
Where this runs
tested against v0.0.3 · 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
py 3.10
✕ build_error
1/2 runs
py 3.9
✕ build_error
1/2 runs
108MB installed
● package 108MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Producer
✓ from confluent_kafka import Producer
✗ from confluent_kafka_stubs import Producer
This quickstart demonstrates a simple producer and consumer using `confluent-kafka`. To leverage the `confluent-kafka-stubs` for type checking, ensure both `confluent-kafka` and `confluent-kafka-stubs` are installed in your environment. You can then run a static analysis tool like MyPy on your code to benefit from the provided type hints.
import os
from confluent_kafka import Producer, Consumer, KafkaException
def delivery_report(err, msg):
if err is not None:
print(f"Message delivery failed: {err}")
else:
print(f"Message delivered to {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}")
def run_example():
# Configuration for Kafka (example with local Kafka or Confluent Cloud)
# Replace with your actual Kafka broker(s)
broker = os.environ.get('KAFKA_BROKER', 'localhost:9092')
conf = {
'bootstrap.servers': broker,
# 'sasl.username': os.environ.get('KAFKA_USERNAME'),
# 'sasl.password': os.environ.get('KAFKA_PASSWORD'),
# 'security.protocol': 'SASL_SSL',
# 'sasl.mechanisms': 'PLAIN',
'acks': 'all'
}
topic = 'my_example_topic'
# Producer
print("--- Producer ---")
producer = Producer(conf)
try:
producer.produce(topic, key='key1', value='hello world from stubs!', callback=delivery_report)
producer.flush(10) # Wait for up to 10 seconds for any outstanding messages to be delivered
except KafkaException as e:
print(f"Producer error: {e}")
# Consumer
print("\n--- Consumer ---")
consumer_conf = {
'bootstrap.servers': broker,
'group.id': 'my_consumer_group',
'auto.offset.reset': 'earliest'
}
consumer = Consumer(consumer_conf)
try:
consumer.subscribe([topic])
# Poll for messages for a short duration
msg = consumer.poll(timeout=5.0)
if msg is None:
print("No message received within timeout.")
elif msg.error():
print(f"Consumer error: {msg.error()}")
else:
print(f"Received message: key={msg.key().decode('utf-8')}, value={msg.value().decode('utf-8')}")
except KafkaException as e:
print(f"Consumer error: {e}")
finally:
consumer.close()
if __name__ == '__main__':
# To run this with type checking, save as e.g., 'example.py'
# Then run 'mypy example.py'
# And 'python example.py' (ensure Kafka broker is running)
run_example()
Upgrade
Version history
0.0.3latest on PyPI · released Jun 12, 2023
Audit
Dependencies
confluent-kafkarequiredThis library provides type stubs for 'confluent-kafka'. It is essential to install the actual 'confluent-kafka' library for runtime execution.