kafka-producer-client
Reusable Kafka producer library — install once, use across all your Python projects.
Works in sync scripts, FastAPI, Django, Celery, or any async framework.
Install
# From your private registry / git
pip install py-kafka-producer-client
# Or directly from git
pip install git+https://github.com/your-org/kafka-producer-client.git
Quick Start
1. Sync usage (scripts, Django, Celery workers)
from kafka_producer_client import KafkaProducerClient, KafkaProducerConfig
config = KafkaProducerConfig(
bootstrap_servers="broker1:9092,broker2:9092",
default_topic="user-events",
client_id="my-service",
)
producer = KafkaProducerClient(config)
# Single event
producer.send({"user": "alice", "action": "login"})
# Batch
producer.send_batch([
{"user": "alice", "action": "login"},
{"user": "bob", "action": "signup"},
])
# Flush before exit
producer.flush()
2. Async usage (FastAPI)
from fastapi import FastAPI, Depends
from kafka_producer_client import KafkaProducerConfig
from kafka_producer_client.fastapi import create_lifespan, get_producer
config = KafkaProducerConfig(
bootstrap_servers="broker:9092",
default_topic="api-events",
)
app = FastAPI(lifespan=create_lifespan(config))
@app.post("/events")
async def post_event(producer=Depends(get_producer)):
result = await producer.send_async({"event": "click"})
return result
3. Async usage (standalone asyncio)
import asyncio
from kafka_producer_client import KafkaProducerClient, KafkaProducerConfig
async def main():
client = KafkaProducerClient(KafkaProducerConfig(
bootstrap_servers="broker:9092",
))
client.start_background_poll()
result = await client.send_async(
{"order_id": 123},
topic="orders",
key="order-123",
)
print(result) # {"topic": "orders", "partition": 0, "offset": 42}
client.close()
asyncio.run(main())
Configuration
All fields on KafkaProducerConfig are optional with sensible defaults:
| Field | Default | Description |
|---|---|---|
bootstrap_servers |
localhost:9092 |
Kafka broker addresses |
client_id |
kafka-producer-client |
Client identifier |
default_topic |
None |
Fallback topic if none given at send |
acks |
all |
Broker ack level |
retries |
3 |
Send retry count |
linger_ms |
5 |
Micro-batching delay (ms) |
compression_type |
snappy |
Message compression |
enable_idempotence |
True |
Exactly-once semantics |
batch_size |
16384 |
Batch size in bytes |
batch_num_messages |
100 |
Max messages per batch |
extra |
{} |
Any additional librdkafka config |
Passing extra librdkafka config
config = KafkaProducerConfig(
bootstrap_servers="broker:9092",
extra={
"security.protocol": "SASL_SSL",
"sasl.mechanism": "PLAIN",
"sasl.username": "key",
"sasl.password": "secret",
},
)
API Reference
KafkaProducerClient
| Method | Returns | Description |
|---|---|---|
send(value, ...) |
None |
Fire-and-forget produce |
send_batch(events) |
int |
Produce many, returns count |
send_async(value) |
dict |
Await broker ACK, returns offset metadata |
send_batch_async() |
list[dict] |
Concurrent async batch |
flush(timeout) |
int |
Block until queue drains |
close() |
None |
Flush + stop poll thread |
start_background_poll() |
None |
Start daemon poll thread |
queue_length |
int (property) |
Messages still in internal queue |
Metadata
Release files for py-kafka-producer-client 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| py_kafka_producer_client-0.1.7.tar.gz | 12.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_kafka_producer_client-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.8 kB
Release files / py_kafka_producer_client-0.1.7.tar.gz
| Download URL | py_kafka_producer_client-0.1.7.tar.gz |
|---|---|
| Size | 12.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8a547de27f3c76af1d8be6951389c33e8fab5faf5d886eff4caa6a1b321ee1e
|
|
BLAKE2b-256 checksum How to use checksums |
8764a0143c2643e1cc588da680e0f2a919c3afdcf7fa938cc2fdf31aaf845e3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / py_kafka_producer_client-0.1.7-py3-none-any.whl
| Download URL | py_kafka_producer_client-0.1.7-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e75c18e573252d03f4141ac2f10ec18d84fa73abb53d7b62fd5896a83a3ae32e
|
|
BLAKE2b-256 checksum How to use checksums |
7a8d02ef366de6849f010f685b5f20a464242d7dc29666af5da74c77d8ceba35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Security for py-kafka-producer-client 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.