Azure Functions Python Library
The `azure-functions` Python library provides the core programming model for developing serverless functions on Azure Functions. It enables developers to write event-driven code in Python that scales automatically and runs in response to various triggers (e.g., HTTP requests, timer, queue messages). This library, currently at version 2.0.0, is designed for the Python v2 programming model and is typically used with Azure Functions runtime v4.x. It receives updates in alignment with the Azure Functions platform's development cycle, which often includes features, performance improvements, and security patches.
Warnings
- breaking The Python v2 programming model (used by `azure-functions` 2.0.0+) introduces significant breaking changes from v1. This shifts from using `function.json` files and a strict 'one function per folder' structure to a Python-first, decorator-based approach. Configuration for triggers and bindings is now directly within Python code.
- deprecated Azure Functions Runtime versions 2.x and 3.x are End-of-Life (EOL) and no longer supported. While apps on these runtimes may still function, they will not receive new features, security patches, or performance optimizations.
- gotcha Cold starts can significantly impact performance on the Consumption Plan. When a function app goes idle, Azure deallocates resources, leading to a delay (cold start) on the next invocation.
- gotcha Default function timeouts on the Consumption Plan are 5 minutes and cannot be changed. Long-running tasks will be terminated.
- gotcha Repeatedly instantiating network clients (e.g., `HttpClient`, database connections) within a function's execution path can lead to `Socket Exception` errors due to resource exhaustion and inefficient connection management.
- gotcha Function App host names (which derive from the app name) have a maximum length of 32 characters. Longer names can lead to host ID collisions, especially when sharing a storage account, which can impede scaling and lease management.
Install
-
pip install azure-functions
Imports
- func
import azure.functions as func
- FunctionApp
app = func.FunctionApp()
- HttpRequest
req: func.HttpRequest
- HttpResponse
func.HttpResponse(...)
Quickstart
import azure.functions as func
import logging
import os
# Instantiate the FunctionApp object
app = func.FunctionApp()
# Define an HTTP trigger function using a decorator
@app.route(route="http_example", methods=["GET", "POST"])
def http_example(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Get name from query parameters or request body
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body for a personalized response.",
status_code=200
)