Skip to main content

Create distributed ASGI applications that pull events from a central Redis queue.

Project description

Distributed ASGI

Uses Redis to distribute ASGI messages between worker ASGI apps based on routes. Workers can be on different machines, they just must be able to connect to the central Redis server.


Routes are made of two parts: a regular expression that matches a path, and a key template string that is used to form the key that ASGI events are pushed to.

Key templates can also use numbered regex backslash replacements. For example, the route {"/api/([a-z-]+)":r"API-\1"} will match and produce the following keys:

PATH                KEY
/api/test           API-test
/api/test/38        API-test
/api/banana         API-banana
/test/api/test2     API-test2

Here's an example:

from distributed_asgi import create_path_distributor

app = create_path_distributor(
    host="",       # point to redis server
        "/api/([a-z-]+)": r"ASGI-\1",
        "/worker/([0-9]+)": r"worker-queue-\1",
        "/": "ALL-WEBSITE-TRAFFIC"

To actually make use of this, we need to make a worker that listens on one or more of the keys

from distributed_asgi import Node

class ASGIApp:
    def __init__(self, scope):
        self.scope = scope

    async def __call__(self, receive, send):
        await send({
            "type": "http.response.start",
            "status": 200

        await send({
            "type": "http.response.body",
            "body": b"Hello World!"

node = Node(

print(f"Starting worker")

Once you have and, use some interface server to run

$ uvicorn server:App

and run as a normal python script:

$ python

The worker will only respond to http requests with a path that contains /api/testing because that is the only key we told it to listen to.

What happens if there are no workers?

If there is no Node instance listening on a key that the Distributor pushes events to, the Distributor will timeout after 5 seconds, close the connection, and return a 405 error. Workers do not need to respond within 5 seconds, the Node class will automatically let the Distributor know there is a worker at least listening.

Project details

Download files

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

Files for distributed-asgi, version 0.0.7
Filename, size & hash File type Python version Upload date
distributed-asgi-0.0.7.tar.gz (4.4 kB) View hashes Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page