Reputation: 11
We are trying to create a single Azure Function App in Python that combines a FastAPI framework for building a backend API and Azure Durable Functions for handling long-running processes. In the example code below, a FastAPI application is instantiated on line 7. This application has multiple internal routes and is working as expected, providing a stable and reliable backend API.
We also want to integrate Azure Durable Functions for long-running tasks. As shown on line 9, we're attempting to instantiate a durable function named myApp.
we would also want to know the implication of doing this as durable functions can be blocking actions and fast api would be asynchronous actions mostly.
we tried to split these into two function app and its working fine, we are trying to check if it is possible to run in single function app in Azure.
Upvotes: 0
Views: 149
Reputation: 3781
It is possible to instantiate more than one top-level function app instance in Azure Functions. Each instance can be configured with its own set of functions, pricing plan, deployment method, and runtime version.
Regarding the specific use case of combining FastAPI and Azure Durable Functions in a single function app, it is possible to do so.
You will need a main.py to contain the FastAPI application instance (in my case- app)
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello from FastAPI"}
Create a function app
import json
import azure.functions as func
import azure.durable_functions as DurableFunc
from app.main import app as fastapi_app
app = func.AsgiFunctionApp(app=fastapi_app, http_auth_level=func.AuthLevel.ANONYMOUS)
myApp = DurableFunc.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@myApp.route("orchestrators/{functionName}")
@myApp.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
function_name = req.route_params.get('functionName')
if req.get_body():
request_body = json.loads(req.get_body().decode())
else:
request_body = None
instance_id = await client.start_new(function_name, client_input=request_body)
response = client.create_check_status_response(req, instance_id)
return response
Create a host.json and local.settings.json
host.json
{
"version": "2.0",
"extensions": {
"durableTask": {
"hubName": "MyTaskHub"
}
}
}
local.setting
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}
Create your Durable Functions
import azure.durable_functions as df
def orchestrator_function(context: df.DurableOrchestrationContext):
result = yield context.call_activity('Hello', "world")
return result
main = df.Orchestrator.create(orchestrator_function)
Create an Activity Function
import azure.functions as func
def main(name: str) -> str:
return f"Hello {name}!"
You can verify the same-
uvicorn main:app --reload
You can now deploy the same to your function app
func azure functionapp publish arkoFuncApp
References:
Upvotes: -1