Skip to main content

Process Redis Events - Python

A Python library for processing Redis Stream events with consumer groups, providing robust event processing with automatic retries, dead letter queues, and OpenTelemetry integration.

Features

  • Type-Safe: Full type hints and mypy compatibility
  • Consumer Groups: Built-in support for Redis consumer groups
  • Automatic Retries: Configurable retry logic with dead letter queue
  • Heartbeat Management: Automatic lease extension for long-running tasks
  • Telemetry: OpenTelemetry integration for metrics and tracing
  • Event Streaming: Optional event stream for progress tracking
  • Async/Await: Built on asyncio for high performance

Synchronous batch consumer (BatchConsumer, StreamConsumer)

For services whose work is blocking (Postgres, Milvus, model inference, paid API calls) there's a threaded consumer with the semantics of the TypeScript consumers (process_redis_events.consumer; see its docstring):

import threading
import redis
from process_redis_events import BatchConsumer, Paused, TransientError, install_signal_handlers

stop = threading.Event()
install_signal_handlers(stop)  # SIGTERM/SIGINT: finish the current batch, then return

def handle(payloads: list[dict]) -> None | list[Exception | None]:
    ...  # raise to fail the batch, or return an error (or None) per payload

BatchConsumer(
    redis.Redis(host="redis", socket_timeout=30),
    "product-enrich",           # stream:product-enrich
    "product-enrich",           # consumer group
    "product-enrich",           # service: in logs and the default consumer name
    handle,
    batch_size=500, max_wait_s=30, lease_ms=10 * 60_000, max_attempts=3, stop=stop,
).run()
  • Consumer name: STREAM_CONSUMER_NAME, else <HOSTNAME>-<service>. On start it takes over its own pending messages at once (a redelivery each), so give a single-replica Deployment (strategy Recreate) a fixed name.
  • Transient errors (transient_kind: Redis, Postgres SQLSTATEs, Milvus/gRPC, HTTP 5xx/429, socket errors, TransientError) are retried in place for up to transient_max_s (15 min) without using up a delivery; then the batch fails whole. Paused(wait_s=...) keeps the batch and waits.
  • Permanent errors bisect the batch down to the bad message; after max_attempts deliveries it goes to stream:dlq:<name> with _dlqMetadata (acked only once that's written).
  • Leases are heartbeated every lease/3, only for messages this consumer still owns.
  • The blocking read stays below the client's socket_timeout (redis-py 8 defaults it to 5 s).
  • StreamConsumer is the same, one message at a time (handler(payload)).

The wire format both libraries write and read is pinned by ../fixtures/stream-format.json.

Installation

pip install process-redis-events

For development:

pip install process-redis-events[dev]

Quick Start

import asyncio
from redis.asyncio import Redis
from process_redis_events import Stream, StartFrom

# Define your data type
from typing import TypedDict

class MyData(TypedDict):
    foo: str

# Create a stream
async def main():
    stream = Stream[MyData](
        name="my-stream",
        create_redis=lambda: Redis.from_url("redis://localhost"),
        produce_events=True
    )

    # Add data to the stream
    await stream.add({"foo": "bar"})

    # Process events
    async def process_item(item):
        print(f"Processing: {item.data}")
        # Your processing logic here

    await stream.process(
        options={
            "consumer_group": "my-group",
            "signal": asyncio.Event(),  # Use for graceful shutdown
        },
        callback=process_item
    )

asyncio.run(main())

Advanced Usage

With Data Transformation

from process_redis_events import QueueItem

async def transform(data_list: list[MyData]) -> list[dict]:
    return [{"bar": item["foo"]} for item in data_list]

async def process_item(item: QueueItem[dict]):
    print(f"Transformed data: {item.data}")
    await item.report_progress(0.5, "Halfway done")
    await item.report_progress(1.0, "Complete")

await stream.process(
    options={
        "consumer_group": "my-group",
        "map": transform,
        "signal": shutdown_event,
        "batch_size": 20,
        "concurrency": 10,
        "lease_ms": 30000,
    },
    callback=process_item
)

Error Handling and Retries

def should_retry(attempt: int, data: MyData) -> bool:
    # Retry up to 3 times
    return attempt < 3

await stream.process(
    options={
        "consumer_group": "my-group",
        "should_retry": should_retry,
        "signal": shutdown_event,
    },
    callback=process_item
)

API Reference

Stream[T]

Main class for processing Redis streams.

Constructor Parameters:

  • name: Stream name
  • create_redis: Factory function returning Redis client
  • produce_events: Enable event stream (default: False)
  • telemetry_config: Optional telemetry configuration

Methods:

  • add(data: T) -> dict: Add item to stream
  • process(options, callback): Process stream items
  • clear(): Clear stream data
  • get_stream_info(): Get stream statistics
  • get_consumer_groups_info(): Get consumer group info
  • create_event_stream(): Create event tracking stream

QueueItem[T]

Represents a queue item being processed.

Attributes:

  • id: Unique message ID
  • data: Message data (type T)
  • attempts: Number of processing attempts
  • report_progress: Async function to report progress

StartFrom

Enum for consumer group starting position:

  • StartFrom.OLDEST: Start from beginning
  • StartFrom.LATEST: Start from latest

Testing

# Run tests
pytest

# Run with coverage
pytest --cov=process_redis_events

# Type checking
mypy process_redis_events

License

ISC License - see LICENSE file for details

Contributing

Contributions welcome! Please ensure:

  • All tests pass
  • Code is type-checked with mypy
  • Code is formatted with black
  • Follow existing patterns and conventions

Metadata

Release files for process-redis-events 5.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for process-redis-events 5.2.0
File Size Uploaded Provenance Actions
process_redis_events-5.2.0.tar.gz 67.1 kB Without provenance Details for process_redis_events-5.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for process-redis-events 5.2.0
File Interpreter ABI Platform Actions
process_redis_events-5.2.0-py3-none-any.whl Python 3 none any Details for process_redis_events-5.2.0-py3-none-any.whl

Total release size: 105.6 kB

Release files / process_redis_events-5.2.0.tar.gz

Download URL process_redis_events-5.2.0.tar.gz
Size 67.1 kB
Tags Source
SHA-256 checksum
How to use checksums
39dbb66509bde5a76eac35fbd6a421d53e78e42919126a19cdbe21f2cc2e501f
BLAKE2b-256 checksum
How to use checksums
f77f8858343d4f76884290b13e598dfef0a25811f4d1273ff0c1259267546022
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / process_redis_events-5.2.0-py3-none-any.whl

Download URL process_redis_events-5.2.0-py3-none-any.whl
Size 38.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2fdb08079cafc8f35cc638e071104a8d732a1f11060b1e0eb1bf392e5d22ff1
BLAKE2b-256 checksum
How to use checksums
25adaf8ca250c22a85e84f629278c412e894d90c08cb79a4c60f7e1f1c91f5b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for process-redis-events 5.2.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page