Skip to main content

Django application to produce/consume events from Kafka

Project description

django-streams

Django application to produce/consume events from Kafka supported by kstreams

Build status codecov python version

django streaming

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_streams-3.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_streams-3.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file django_streams-3.1.0.tar.gz.

File metadata

  • Download URL: django_streams-3.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Linux/6.17.0-1020-azure

File hashes

Hashes for django_streams-3.1.0.tar.gz
Algorithm Hash digest
SHA256 1f91fc8a33e3133dd753e37addf6f1e1002bec3133f9ef31e6c7c507d9cc7cef
MD5 6f41907be0db91fc65214d63cbb3af1c
BLAKE2b-256 490f6eee9a5cba15227c4dfcd5d6b1e45b5fc2059c2af326eeec94b618df1ce5

See more details on using hashes here.

File details

Details for the file django_streams-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_streams-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Linux/6.17.0-1020-azure

File hashes

Hashes for django_streams-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c49901d2ea4dc8e962ec3bf57bb8a279daf1d2ee9ed30ba27c92970556615a8
MD5 8b920abd54cde0b08cedcd77e9c3efbf
BLAKE2b-256 6c71f884db95469976ed42bd5c05f40ad0281b42dd04ccf4d0094d652e69321a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page