Durable Functions for Python

1.5.0 · active · verified Mon Apr 13

Durable Functions is an extension of Azure Functions that enables developers to write stateful functions in a serverless environment. It allows defining stateful workflows using orchestrator functions and stateful entities with entity functions. The extension automatically manages state, checkpoints, and restarts, abstracting away complex state management concerns. The current stable version is 1.5.0, with ongoing active development and regular releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic 'Hello World' Durable Functions application using the Python v2 programming model and blueprints. It includes an HTTP-triggered starter function, an orchestrator function that calls an activity function multiple times in parallel (fan-out/fan-in pattern), and the activity function itself. To run this, you would typically set up an Azure Functions project locally, install `azure-functions-durable`, and ensure `AzureWebJobsStorage` is configured (e.g., to `UseDevelopmentStorage=true` for local Azurite).

import logging
import os
import azure.functions as func
import azure.durable_functions as df

# Instantiate a Durable Functions Blueprint
bp = df.Blueprint()

# An HTTP-triggered function that starts an instance of the orchestration
@bp.route(route="startOrchestrator")
@bp.durable_client_input(client_name="client")
async def start_orchestrator(req: func.HttpRequest, client: df.DurableOrchestrationClient):
    orchestration_id = await client.start_new("my_orchestrator", None, "World")
    logging.info(f"Started orchestration with ID = '{orchestration_id}'.")
    return client.create_check_status_response(req, orchestration_id)

# The orchestrator function, which orchestrates calls to other functions
@bp.orchestration_trigger(context_name="context")
def my_orchestrator(context: df.DurableOrchestrationContext):
    # The orchestrator is deterministic, so use context.call_activity for side-effects
    result1 = yield context.call_activity('say_hello', "Tokyo")
    result2 = yield context.call_activity('say_hello', "Seattle")
    result3 = yield context.call_activity('say_hello', "London")
    return [result1, result2, result3]

# An activity function, which performs the actual work
@bp.activity_trigger(input_name="city")
def say_hello(city: str) -> str:
    logging.info(f"Saying hello to {city}.")
    return f"Hello {city}!"

# In your main function_app.py, register the blueprint:
# import azure.functions as func
# from your_module_name import bp
# app = func.FunctionApp()
# app.register_functions(bp)

view raw JSON →