Skip to main content

Develop reliable distributed systems on the Dispatch platform.

Project description

dispatch-py

Docs PyPI Test PyPI version Reference

Python package to develop applications with the Dispatch platform.

What is Dispatch?

Dispatch is a platform for developing scalable & reliable distributed systems.

Dispatch provides a simple programming model based on Distributed Coroutines, allowing complex, dynamic workflows to be expressed with regular code and control flow.

Dispatch schedules function calls across a fleet of service instances, incorporating fair scheduling, transparent retry of failed operations, and durability.

To get started, follow the instructions to sign up for Dispatch 🚀.

Installation

This package is published on PyPI as dispatch-py, to install:

pip install dispatch-py

Usage

The SDK allows Python applications to declare functions that Dispatch can orchestrate:

@dispatch.function
def action(msg):
    ...

The @dispatch.function decorator declares a function that can be run by Dispatch. The call has durable execution semantics; if the function fails with a temporary error, it is automatically retried, even if the program is restarted, or if multiple instances are deployed.

The SDK adds a method to the action object, allowing the program to dispatch an asynchronous invocation of the function; for example:

action.dispatch('hello')

Configuration

In order for Dispatch to interact with functions remotely, the SDK needs to be configured with the address at which the server can be reached. The Dispatch API Key must also be set, and optionally, a public signing key should be configured to verify that requests originated from Dispatch. These configuration options can be passed as arguments to the the Dispatch constructor, but by default they will be loaded from environment variables:

Environment Variable Value Example
DISPATCH_API_KEY d4caSl21a5wdx5AxMjdaMeWehaIyXVnN
DISPATCH_ENDPOINT_URL https://service.domain.com
DISPATCH_VERIFICATION_KEY -----BEGIN PUBLIC KEY-----...

Finally, the Dispatch instance needs to mount a route on a HTTP server in to receive requests from Dispatch. At this time, the SDK integrates with FastAPI; adapters for other popular Python frameworks will be added in the future.

Integration with FastAPI

The following code snippet is a complete example showing how to install a Dispatch instance on a FastAPI server:

from fastapi import FastAPI
from dispatch.fastapi import Dispatch
import requests

app = FastAPI()
dispatch = Dispatch(app)

@dispatch.function
def publish(url, payload):
    r = requests.post(url, data=payload)
    r.raise_for_status()

@app.get('/')
def root():
    publish.dispatch('https://httpstat.us/200', {'hello': 'world'})
    return {'answer': 42}

In this example, GET requests on the HTTP server dispatch calls to the publish function. The function runs concurrently to the rest of the program, driven by the Dispatch SDK.

The instantiation of the Dispatch object on the FastAPI application automatically installs the HTTP route needed for Dispatch to invoke functions.

Local Testing

Mock Dispatch

The SDK ships with a mock Dispatch server. It can be used to quickly test your local functions, without requiring internet access.

Note that the mock Dispatch server has very limited scheduling capabilities.

python -m dispatch.test $DISPATCH_ENDPOINT_URL

The command will start a mock Dispatch server and print the configuration for the SDK.

For example, if your functions were exposed through a local endpoint listening on http://127.0.0.1:8000, you could run:

$ python -m dispatch.test http://127.0.0.1:8000
Spawned a mock Dispatch server on 127.0.0.1:4450

Dispatching function calls to the endpoint at http://127.0.0.1:8000

The Dispatch SDK can be configured with:

  export DISPATCH_API_URL="http://127.0.0.1:4450"
  export DISPATCH_API_KEY="test"
  export DISPATCH_ENDPOINT_URL="http://127.0.0.1:8000"
  export DISPATCH_VERIFICATION_KEY="Z+nTe2VRcw8t8Ihx++D+nXtbO28nwjWIOTLRgzrelYs="

Real Dispatch

To test local functions with the production instance of Dispatch, it needs to be able to access your local endpoint.

A common approach consists of using ngrok to setup a public endpoint that forwards to the server running on localhost.

For example, assuming the server is running on port 8000 (which is the default with FastAPI), the command to create a ngrok tunnel is:

ngrok http http://localhost:8000

Running this command opens a terminal interface that looks like this:

ngrok

Build better APIs with ngrok. Early access: ngrok.com/early-access

Session Status                online
Account                       Alice (Plan: Free)
Version                       3.6.0
Region                        United States (California) (us-cal-1)
Latency                       -
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://f441-2600-1700-2802-e01f-6861-dbc9-d551-ecfb.ngrok-free.app -> http://localhost:8000

To configure the Dispatch SDK, set the endpoint URL to the endpoint for the Forwarding parameter; each ngrok instance is unique, so you would have a different value, but in this example it would be:

export DISPATCH_ENDPOINT_URL="https://f441-2600-1700-2802-e01f-6861-dbc9-d551-ecfb.ngrok-free.app"

Distributed Coroutines for Python

The @dispatch.function decorator can also be applied to Python coroutines (a.k.a. async functions), in which case each await point becomes a durability step in the execution. If the awaited operation fails, it is automatically retried, and the parent function is paused until the result are available or a permanent error is raised.

@dispatch.function
async def pipeline(msg):
    # Each await point is a durability step, the functions can be run across the
    # fleet of service instances and retried as needed without losing track of
    # progress through the function execution.
    msg = await transform1(msg)
    msg = await transform2(msg)
    await publish(msg)

@dispatch.function
async def publish(msg):
    # Each dispatch function runs concurrently to the others, even if it does
    # blocking operations like this POST request, it does not prevent other
    # concurrent operations from carrying on in the program.
    r = requests.post("https://somewhere.com/", data=msg)
    r.raise_for_status()

@dispatch.function
async def transform1(msg):
    ...

@dispatch.function
async def transform2(msg):
    ...

This model is composable and can be used to create fan-out/fan-in control flows. gather can be used to wait on multiple concurrent calls:

from dispatch import gather

@dispatch.function
async def process(msgs):
    concurrent_calls = [transform(msg) for msg in msgs]
    return await gather(*concurrent_calls)

@dispatch.function
async def transform(msg):
    ...

Dispatch converts Python coroutines to Distributed Coroutines, which can be suspended and resumed on any instance of a service across a fleet.

Serialization

Dispatch uses the pickle library to serialize coroutines.

Serialization of coroutines is enabled by a CPython extension.

The user must ensure that the contents of their stack frames are serializable. That is, users should avoid using variables inside coroutines that cannot be pickled.

If a pickle error is encountered, serialization tracing can be enabled with the DISPATCH_TRACE=1 environment variable to debug the issue. The stacks of coroutines and generators will be printed to stdout before the pickle library attempts serialization.

For help with a serialization issues, please submit a GitHub issue.

Examples

Check out the examples directory for code samples to help you get started with the SDK.

Contributing

Contributions are always welcome! Would you spot a typo or anything that needs to be improved, feel free to send a pull request.

Pull requests need to pass all CI checks before getting merged. Anything that isn't a straightforward change would benefit from being discussed in an issue before submitting a change.

Remember to be respectful and open minded!

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

dispatch-py-0.3.0.tar.gz (73.6 kB view hashes)

Uploaded Source

Built Distributions

dispatch_py-0.3.0-cp312-cp312-win_amd64.whl (93.3 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

dispatch_py-0.3.0-cp312-cp312-win32.whl (92.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

dispatch_py-0.3.0-cp312-cp312-musllinux_1_1_x86_64.whl (113.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

dispatch_py-0.3.0-cp312-cp312-musllinux_1_1_s390x.whl (113.2 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ s390x

dispatch_py-0.3.0-cp312-cp312-musllinux_1_1_ppc64le.whl (114.6 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ppc64le

dispatch_py-0.3.0-cp312-cp312-musllinux_1_1_i686.whl (112.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

dispatch_py-0.3.0-cp312-cp312-musllinux_1_1_aarch64.whl (113.5 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

dispatch_py-0.3.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (109.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

dispatch_py-0.3.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (110.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

dispatch_py-0.3.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (109.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

dispatch_py-0.3.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (109.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

dispatch_py-0.3.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (107.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

dispatch_py-0.3.0-cp312-cp312-macosx_11_0_arm64.whl (91.3 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

dispatch_py-0.3.0-cp312-cp312-macosx_10_9_x86_64.whl (91.0 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

dispatch_py-0.3.0-cp311-cp311-win_amd64.whl (93.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

dispatch_py-0.3.0-cp311-cp311-win32.whl (92.7 kB view hashes)

Uploaded CPython 3.11 Windows x86

dispatch_py-0.3.0-cp311-cp311-musllinux_1_1_x86_64.whl (112.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

dispatch_py-0.3.0-cp311-cp311-musllinux_1_1_s390x.whl (111.9 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ s390x

dispatch_py-0.3.0-cp311-cp311-musllinux_1_1_ppc64le.whl (113.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ppc64le

dispatch_py-0.3.0-cp311-cp311-musllinux_1_1_i686.whl (111.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

dispatch_py-0.3.0-cp311-cp311-musllinux_1_1_aarch64.whl (112.4 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

dispatch_py-0.3.0-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (107.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

dispatch_py-0.3.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (109.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

dispatch_py-0.3.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (108.3 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

dispatch_py-0.3.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (107.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

dispatch_py-0.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (106.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

dispatch_py-0.3.0-cp311-cp311-macosx_11_0_arm64.whl (91.2 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

dispatch_py-0.3.0-cp311-cp311-macosx_10_9_x86_64.whl (90.9 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

Supported by

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