Skip to main content

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.

Usage

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:

# distributor.py
from distributed_asgi import create_path_distributor

app = create_path_distributor(
    host="mywebsite.com",       # point to redis server
    port=6379,
    db=0,
    password="abc123",
    routes={
        "/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

# worker.py
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(
    host="mywebsite.com",
    port="6379",
    password="abc123",
    cls=ASGIApp,
    key='ASGI-testing'
)

print(f"Starting worker")
node.run()

Once you have worker.py and server.py, use some interface server to run distributor.py.

$ uvicorn server:App

and run worker.py as a normal python script:

$ python worker.py

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.

Metadata

Release files for distributed-asgi 0.0.7

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

Source distribution (sdist)

Source distribution for distributed-asgi 0.0.7
File Size Uploaded Provenance Actions
distributed-asgi-0.0.7.tar.gz 4.4 kB Without provenance Details for distributed-asgi-0.0.7.tar.gz

Release files / distributed-asgi-0.0.7.tar.gz

Download URL distributed-asgi-0.0.7.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f6855711b2b8d39ac1315657307377247676548d0ae4155396a8ae365ce3715a
BLAKE2b-256 checksum
How to use checksums
c5c4b4adc38e5f44d4f4b5a026f11298613e7237d1094b0d8863869d6cf3e423
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.19.6 CPython/3.6.4

Security for distributed-asgi 0.0.7

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