NOTE: This repo houses a hackathon prototype. For the actual, officially-supported Durable Functions for Python library, please visit https://github.com/Azure/azure-functions-durable-python
The azure-functions-durable
pip package allows you to write Durable Functions for Python(https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node). Durable Functions is an extension of Azure Functions that lets you write stateful functions and workflows in a serverless environment. The extension manages state, checkpoints, and restarts for you. Durable Functions' advantages include:
- Define workflows in code. No JSON schemas or designers are needed.
- Call other functions synchronously and asynchronously. Output from called functions can be saved to local variables.
- Automatically checkpoint progress whenever the function schedules async work. Local state is never lost if the process recycles or the VM reboots.
You can find more information at the following links:
A durable function, or orchestration, is a solution made up of different types of Azure Functions:
- Activity: the functions and tasks being orchestrated by your workflow.
- Orchestrator: a function that describes the way and order actions are executed in code.
- Client: the entry point for creating an instance of a durable orchestration.
Durable Functions' function types and features are documented in-depth here.
You can follow the instructions below to get started with a function chaining example, or follow the general checklist below:
-
Install prerequisites:
- Azure Functions Core Tools version 2.x
- Azure Storage Emulator (Windows) or an actual Azure storage account (Mac or Linux)
- Python 3.6 or later
-
Install the Durable Functions extension
Run this command from the root folder of your Azure Functions app:
func extensions install -p Microsoft.Azure.WebJobs.Extensions.DurableTask -v 1.8.3
durable-functions requires Microsoft.Azure.WebJobs.Extensions.DurableTask 1.7.0 or greater.
- Install the
azure-durable-functions
pip package at the root of your function app:
Create and activate virtual environment
python3 -m venv env
source env/bin/activate
pip install azure-durable-functions
- Write an activity function (see sample):
def main(name: str) -> str:
logging.info(f"Activity Triggered: {name}")
# your code here
- Write an orchestrator function (see sample):
def main(context: str):
orchestrate = df.Orchestrator.create(generator_function)
result = orchestrate(context)
return result
Note: Orchestrator functions must follow certain code constraints.
- Write your client function (see sample):
TBD
Note: Client functions are started by a trigger binding available in the Azure Functions 2.x major version. Read more about trigger bindings and 2.x-supported bindings.
The Durable Functions samples demonstrate several common use cases. They are located in the samples directory. Descriptive documentation is also available:
- Function Chaining - Hello Sequence
- Fan-out/Fan-in - Cloud Backup
- Monitors - Weather Watcher
- Human Interaction & Timeouts - Phone Verification
def generator_function(context):
outputs = []
task1 = yield context.df.callActivity("DurableActivity", "One")
task2 = yield context.df.callActivity("DurableActivity", "Two")
task3 = yield context.df.callActivity("DurableActivity", "Three")
outputs.append(task1)
outputs.append(task2)
outputs.append(task3)
return outputs