django-streams
Django application to produce/consume events from Kafka supported by kstreams
Installation
pip install django-streams
or with poetry
poetry add django-streams
and add it to INSTALLED_APPS:
INSTALLED_APPS = [
...
"django_streams",
...
"my_streams_app",
# etc...
]
Documentation
https://kpn.github.io/django-streams/
Usage
create the engine:
# my_streams_app/engine.py
from django_streams import create_engine
from kstreams.backends import Kafka
stream_engine = create_engine(
title="test-engine",
backend=Kafka(),
)
To configure the backend follow the kstreams backend documentation
Consuming events
Define your streams:
# my_streams_app/streams.py
from kstreams import ConsumerRecord
from .engine import stream_engine
@stream_engine.stream("dev-kpn-des--hello-kpn", group_id="django-streams-principal-group-id") # your consumer
async def consumer_task(cr: ConsumerRecord):
async for cr in stream:
logger.info(f"Event consumed: headers: {cr.headers}, value: {cr.value}")
and then in your apps.py you must import the python module or your coroutines
# my_streams_app/apps.py
from django.apps import AppConfig
class StreamingAppConfig(AppConfig):
name = "streaming_app"
def ready(self):
from . import streams # import the streams module
Now you can run the worker:
python manage.py worker
Producing events
Producing events can be sync or async. If you are in a sync context you must use stream_engine.sync_send, otherwise stream_engine.send. For both cases a RecordMetadata is returned.
# streaming_app/views.py
from django.http import HttpResponse
from django.views.generic import View
from .engine import stream_engine
class HelloWorldView(View):
def get(self, request, *args, **kwargs):
record_metadata = stream_engine.sync_send(
"hello-kpn",
value=b"hello world",
key="hello",
partition=None,
timestamp_ms=None,
headers=None,
)
return HttpResponse(f"Event metadata: {record_metadata}")
Benchmark
Producer:
| Total produced events | Time (seconds) |
|---|---|
| 1 | 0.004278898239135742 |
| 10 | 0.030963897705078125 |
| 100 | 0.07049298286437988 |
| 1000 | 0.6609988212585449 |
| 10000 | 6.501222133636475 |
Running tests
./scrtips/test
Code formating
./scrtips/format
Metadata
Release files for django-streams 3.1.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 | |
|---|---|---|---|---|
| django_streams-3.1.0.tar.gz | 9.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_streams-3.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.3 kB
Release files / django_streams-3.1.0.tar.gz
| Download URL | django_streams-3.1.0.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f91fc8a33e3133dd753e37addf6f1e1002bec3133f9ef31e6c7c507d9cc7cef
|
|
BLAKE2b-256 checksum How to use checksums |
490f6eee9a5cba15227c4dfcd5d6b1e45b5fc2059c2af326eeec94b618df1ce5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.14.6 Linux/6.17.0-1020-azure
|
Release files / django_streams-3.1.0-py3-none-any.whl
| Download URL | django_streams-3.1.0-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4c49901d2ea4dc8e962ec3bf57bb8a279daf1d2ee9ed30ba27c92970556615a8
|
|
BLAKE2b-256 checksum How to use checksums |
6c71f884db95469976ed42bd5c05f40ad0281b42dd04ccf4d0094d652e69321a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.14.6 Linux/6.17.0-1020-azure
|
Security for django-streams 3.1.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.