Forum Discussion

Egon_1's avatar
Egon_1
Copper Contributor
Aug 30, 2024

Azure Functions - Using durable functions in Python v2

Hey guys, i am new to azure functions. Maybe one expert can have a quick look at the code and give me some advice, since I really dont know whats going on. I want to use a simple durable function whith python v2 in vs code.
I have the azure extensions, Azure functions core tools, etc. installed. I find it hard to read the documentation, I am missing more examples and explanations. I wrote the following example of reading a book.
The Skript ist starting and I get logs, but it doesnt work as planed. The orchestrator function does start again at the top (not in the while loop). 

 

How it should work:
http_start trigger: Starts the orchestrator function
orchestrator function: Manages the book reading process
activity function: flips the page

 

 

import azure.functions as func
import azure.durable_functions as df
import logging.config
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):
    x = None
    book = "Example_book"
    page = 1
    logging.warning(f'Your Book: {book} at page {page}')
    while page < 4:
        x = yield context.call_activity("flip_page", {
            "book": book,
            "page": page,
        })
        book = x.get("book")
        page = x.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

 

 

 


Thanks for the help in advance! 


Best regards!

 

  • Egon_1 

     

    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

Resources