Forum Discussion
Azure Functions - Using durable functions in Python v2
How about below? Please make sure you understand before apply:
import azure.functions as func
import azure.durable_functions as df
import logging
app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.orchestration_trigger(context_name="context")
def orchestrator_reading_a_book(context: df.DurableOrchestrationContext):
book = "Example_book"
page = context.get_input() or 1 # Get the initial page or start from 1
logging.warning(f'Your Book: {book} at page {page}')
while page < 4:
result = yield context.call_activity("flip_page", {
"book": book,
"page": page,
})
book = result.get("book")
page = result.get("page")
logging.warning(f'Your Book: {book} at page {page}')
logging.warning("You finished reading the book")
return "You finished reading the book"
@app.activity_trigger(input_name="context")
def flip_page(context):
if context:
book = context["book"]
page = context["page"]
page += 1
logging.warning(f"You flipped the page to page {page}")
return {"book": book, "page": page}
# An HTTP-triggered function with a Durable Functions client binding
@app.route(route="orchestrators/{functionName}")
@app.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
function_name = req.route_params.get('functionName')
instance_id = await client.start_new(function_name)
response = client.create_check_status_response(req, instance_id)
return response