Skip to main content

redispatcher

redispatcher logo

Dispatch and run distributed work asynchronously, brokered by Redis


Documentation: https://rafalstapinski.github.io/redispatcher

Source Code: https://github.com/rafalstapinski/redispatcher


Test Status pypi Supported Python Versions: 3.8, 3.9, 3.10 MIT License

What is redispatcher

redispatcher allows you to dispatch work that needs to be done in a process separate from your main program loop. This is useful in cases when you need to process some long running work that doesn't necessarily need to be done synchronously within your code. A classic example of this is sending a welcome email to a user as they sign up for your service. It's not necessary to wait for the results of sending an email, and it may take a few seconds to do so. redispatcher lets you fire-and-forget this work (as a message put into a Redis server) and have it be executed in the background, asynchronously, in a separate process (or machine) entirely.

redispatcher comes in two parts:

  1. A library that lets you define workers, define strongly typed messages sent to workers, and provides helper functions to facilitate dispatching that work
  2. A daemon that runs your workers in a pool, consistently listening for any incoming messages to be processed

Why use it

There are certainly other solutions for orchestrating distributed workers. redispatcher aims to be super lightweight, very fast and simple to set up (there are many free cloud-hosted Redis solutions available), and has robust type validation and intellisense support.

Features

  • Full intellisense support across your code, despite a distributed workload
  • Strongly typed message contract between your publishing code and consumer
  • Minimal boilerplate required to setup and start publishing compared than most alternatives
  • Minimal performance overhead and completely non-blocking, thanks to asyncio (and works with alternatives like uvloop)

Dependencies

  • aioredis is used under the hood to publish message to and read messages from Redis
  • pydantic is used to to validate messages conform to your strongly typed contracts

Installation

Install with poetry

$ poetry add redispatcher

or with pip

$ pip install redispatcher

Basic Usage

Defining your worker

from redispatcher import BaseConsumer

class SendWelcomeEmail(BaseConsumer):

    QUEUE = "send-welcome-email"

    class Message(BaseConsumer.Message):
        email: str
        name: str
    
    async def process_message(self, message: Message):
        # construct an email and send it to the `message.email` address

Dispatching work

from clients import my_aioredis_client

@app.post("/register")
async def register(...)
    ...
    message = SendWelcomeEmail.Message(email=..., name=..., registered=True)
    await SendWelcomeEmail.dispatch(message, my_aioredis_client)
    ...

Running redispatcher

from redispatcher import Redispatcher, RedispatcherConfig, ConsumerConfig

config = RedispatcherConfig(
    redis_dsn="redis://localhost:6379/0",
    consumers=[
        ConsumerConfig(
            consumer_class=SendWelcomeEmail,
            count=2
        )
    ]
)

if __name__ == "__main__":
    dispatcher = Redispatcher(config)
    dispatcher.start() 

Contributing

redispatcher is already used in production, but is still in its infancy.

If you find a bug, open an issue with a detailed description and steps to reproduce.

If you're looking for a feature, open an issue with a detailed description and use case. Feel free open a pull request if you want to contribure directly!

Metadata

Release files for redispatcher 0.3.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 redispatcher 0.3.0
File Size Uploaded
redispatcher-0.3.0.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for redispatcher 0.3.0
File Interpreter ABI Platform
redispatcher-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / redispatcher-0.3.0.tar.gz

Download URL redispatcher-0.3.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
95771fd9812498e25c7bd02dea4562a3f2f50e25052e12c0163cf16695a960e5
BLAKE2b-256 checksum
How to use checksums
3cfee18f2820daf2fce82d6f71562ed91f55fbd0713206d1d99b7905f078c7fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1019-azure

Release files / redispatcher-0.3.0-py3-none-any.whl

Download URL redispatcher-0.3.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9c2165921f184d23355ab01e30e026b7aa1b2a69b50769b7a5920befe073690
BLAKE2b-256 checksum
How to use checksums
829a6d72b17734c9e95a00dbaef3a30421c4aec58e13c29b60a43aa4cec6896c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1019-azure

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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