Python SDK for Upstash Workflow
Project description
Upstash Workflow SDK
Upstash Workflow lets you write durable, reliable and performant serverless functions. Get delivery guarantees, automatic retries on failure, scheduling and more without managing any infrastructure.
See the documentation for more details
Quick Start
Here, we will briefly showcase how you can get started with Upstash Workflow using FastAPI.
Alternatively, you can check our quickstarts for different frameworks, including FastAPI and Next.js & FastAPI.
Install
First, create a new directory and set up a virtual environment:
python -m venv venv
source venv/bin/activate
Then, install the required packages:
pip install fastapi uvicorn upstash-workflow
Get QStash token
Go to Upstash Console and copy the QSTASH_TOKEN, set it in the .env file.
export QSTASH_TOKEN=
Define a Workflow Endpoint
To declare workflow endpoints, use the @serve.post decorator. Save the following code to main.py:
from fastapi import FastAPI
from upstash_workflow.fastapi import Serve
from upstash_workflow import AsyncWorkflowContext
app = FastAPI()
serve = Serve(app)
# mock function
def some_work(input: str) -> str:
return f"processed '{input}'"
# serve endpoint which expects a string payload:
@serve.post("/example")
async def example(context: AsyncWorkflowContext[str]) -> None:
# get request body:
input = context.request_payload
async def _step1() -> str:
output = some_work(input)
print("step 1 input", input, "output", output)
return output
# run the first step:
result: str = await context.run("step1", _step1)
async def _step2() -> None:
output = some_work(result)
print("step 2 input", result, "output", output)
# run the second step:
await context.run("step2", _step2)
In the example, you can see that steps are declared through the context object.
The kinds of steps which are available are:
context.run: execute a functioncontext.sleep: sleep for some timecontext.sleep_until: sleep until some timestampcontext.call: make a third party call without consuming any runtime
You can learn more about these methods from our documentation.
Run the Server
Upstash Workflow needs a public URL to orchestrate the workflow. Check out our Local Development guide to learn how to set up a local tunnel.
Create the tunnel and set the UPSTASH_WORKFLOW_URL environment variable in the .env file with the public URL:
ngrok http localhost:8000
export UPSTASH_WORKFLOW_URL=
Then, set the environment variables:
source .env
Finally, run the server:
uvicorn main:app --reload
FastAPI server will be running at localhost:8000.
Contributing
Development
- Clone the repository
- Install Poetry
- Install dependencies with
poetry install - Create a .env file with
cp .env.example .envand fill in the environment variables - Run tests with
poetry run pytest - Format with
poetry run ruff format . - Check with
poetry run ruff check . - Type check with
poetry run mypy --show-error-codes .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file upstash_workflow-0.0.1rc5.tar.gz.
File metadata
- Download URL: upstash_workflow-0.0.1rc5.tar.gz
- Upload date:
- Size: 27.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41ca25d3f70dd7aa23cf73481c18a916d7a4ccba171e47573676d2b297bce6f8
|
|
| MD5 |
715009b3bc0c0fc3a40f73d5c6acedab
|
|
| BLAKE2b-256 |
0cd0b03b28f7b82ab2136385c55dda55a61356ea934387335637ac16190d20bc
|
File details
Details for the file upstash_workflow-0.0.1rc5-py3-none-any.whl.
File metadata
- Download URL: upstash_workflow-0.0.1rc5-py3-none-any.whl
- Upload date:
- Size: 39.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.8.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e49f29833987e8f0f204a7491b2733f379e1c7038ce3bd8605efa4b9040beebd
|
|
| MD5 |
4b17611918f3388371460877149d202f
|
|
| BLAKE2b-256 |
90fb02e5e4ce14dd3df177541c6715e072468344da5310e84ac229d95cdd5606
|