WKafka v1.0.0 LTS 🚀
Professional, Decorator-based Kafka Wrapper for Python.
WKafka simplifies Apache Kafka integration by providing a high-level, intuitive API focused on developer productivity. It includes built-in support for complex data types like JSON, YAML, Images, Files, and Pydantic models, making it ideal for modern microservices, IoT, and Computer Vision pipelines.
🌟 Features
- Decorator-driven API: Minimalistic and clean message handling.
- Modern Python: Fully typed, PEP 8 compliant, supporting Python 3.9 through 3.14.
- Enterprise Security: Built-in support for SASL (PLAIN, SCRAM) and KRaft mode.
- Multimedia & File Native: Seamlessly send and receive images (OpenCV/NumPy/PIL) and arbitrary files (PDF, ZIP, TXT) via
format="file". - Type-safe Pydantic Validation: Automatic schema validation with
format="pydantic". - Manual Offset Commit: Control At-Least-Once delivery semantics with
auto_commit=Falseandmsg.commit(). - Immediate Commit on Receive: Built-in support for At-Most-Once / early acknowledgment with
commit_on_receive=True. - Decoupled Background Execution: Non-blocking background worker dispatch with
background=Trueandbackground_workers=Nto prevent Kafka heartbeat timeouts during long-running tasks. - Partition Auto-scaling & Retries: Automatic topic partition scaling with
describe_topicsinspection and exponential backoff retry resilience against transientNodeNotReadyError. - Async/Await Support: Define non-blocking
async defconsumer handlers. - Professional Ops: Structured logging via
loguruand multi-version testing withtox.
📦 Installation
# Via pip
pip install wkafka
# Via poetry
poetry add wkafka
Optional snappy compression:
pip install wkafka[snappy]
🚀 Quick Start
Basic Producer & Consumer
from wkafka import WKafka
# Configures automatically via KAFKA_SERVER or defaults to localhost:9092
kafka = WKafka(bootstrap_servers="localhost:9092")
@kafka.consumer(topic="orders", format="json")
def handle_order(msg):
print(f"New order received: {msg.value}")
# Start consumers in a background thread pool
kafka.run_consumers(block=True)
# Produce with context manager safety
with kafka.producer() as p:
p.send("orders", value={"id": 123, "item": "Coffee"}, format="json")
Manual Offset Commit
@kafka.consumer(topic="transactions", format="json", auto_commit=False)
def handle_tx(msg):
# Process business logic
save_to_db(msg.value)
# Explicitly commit offset only after success
msg.commit()
Retries & DLQ Routing
@kafka.consumer(
topic="unstable_events",
format="json",
max_retries=3,
retry_delay=1.0,
dlq_topic="unstable_events.DLQ"
)
def handle_event(msg):
process_payload(msg.value)
Immediate Commit on Receive (Early Acknowledgment)
@kafka.consumer(topic="events", format="json", commit_on_receive=True)
def handle_early_ack(msg):
# Offset is committed immediately upon receipt before entering this function
process_quick_event(msg.value)
Long-Running Tasks & Decoupled Background Workers
# Perfect for ML inference, video processing, or PDF generation that takes hours
@kafka.consumer(
topic="reports.generate",
format="json",
commit_on_receive=True, # Commit offset immediately upon receipt
background=True, # Offload execution to a background ThreadPool
background_workers=4, # Up to 4 parallel background workers
)
def handle_long_task(msg):
# Runs in a separate thread. Kafka consumer loop never blocks and never triggers max_poll_interval_ms rebalances!
generate_heavy_report_and_pdf(msg.value)
📂 Project Structure
wkafka.core: Orchestration and base logic (WKafka,Message).wkafka.serializers: Extensible serialization system (JSONSerializer,YAMLSerializer,ImageSerializer,PydanticSerializer,FileSerializer).wkafka.controller: Backward compatibility layer for legacy code.examples/: 12 complete, production-ready example modules (01_basicthrough12_pydantic_validation).environment/: Production-ready Docker setups (KRaft, SASL).
🛠️ Tecnologías y Librerías Relevantes
- Python (3.9 - 3.14): Lenguaje principal de desarrollo y ejecución.
- kafka-python-ng / kafka-python: Client library for low-level communication with Apache Kafka.
- OpenCV (
opencv-python) & Pillow: Procesamiento, renderizado, serialización y deserialización de imágenes. - Pydantic: Validación de esquemas y modelos de datos tipados (
format="pydantic"). - NumPy: Manejo de estructuras de datos matriciales multidimensionales para imágenes.
- PyYAML: Serialización y deserialización nativa de estructuras YAML.
- Loguru: Sistema avanzado de logging estructurado.
🧪 Unit Testing & Coverage
To run the unit test suite, calculate coverage, or execute tests inside a sandboxed Docker container:
Run Locally (pytest)
poetry run pytest tests/
Coverage Report (80% Minimum Guaranteed)
./run_coverage.sh
El script genera un informe de cobertura HTML (htmlcov/index.html) garantizando una cobertura mínima global de 80%.
Sandboxed Testing with Docker
./run_tests_docker.sh
👤 Autor & Afiliación Oficial
- William Steve Rodriguez Villamizar (Wisrovi)
- Cargo: Principal AI Engineer & Applied AI Solutions Architect | Scientific Researcher
- 🌐 Portal Oficial: wisrovi.dev
- 💼 LinkedIn: wisrovi-rodriguez
- 🆔 ORCID: 0009-0005-0710-1861
- 📦 PyPI: pypi.org/user/wisrovi/
- 🐙 GitHub: @wisrovi
📜 License
Distributed under the MIT License. Open for industrial and research use.
Metadata
Release files for wkafka 1.4.0
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 | |
|---|---|---|---|---|
| wkafka-1.4.0.tar.gz | 14.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wkafka-1.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.8 kB
Release files / wkafka-1.4.0.tar.gz
| Download URL | wkafka-1.4.0.tar.gz |
|---|---|
| Size | 14.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e2cc74828632f10070c794549c0c7d02f870948dbdc1e6ee4ef2eb6cacecd03
|
|
BLAKE2b-256 checksum How to use checksums |
35a5390cc79df0742f15f1c2fb54dffdff5fa454bd6c0088869fc554120dc8e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.0 CPython/3.12.3 Linux/6.17.0-29-generic
|
Release files / wkafka-1.4.0-py3-none-any.whl
| Download URL | wkafka-1.4.0-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3a457cf3c310187a0739c68dfce31325a64179246ae062dbad5d42b2fb0006a1
|
|
BLAKE2b-256 checksum How to use checksums |
7e66c93ae4e96d114f6c914a24601f9d7fe6ade1922fab20b0b8b4fb3d4a46ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.0 CPython/3.12.3 Linux/6.17.0-29-generic
|
Security for wkafka 1.4.0
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.