Loading...

Azure Functions: V2 Python Programming Model is Generally Available

Azure Functions: V2 Python Programming Model is Generally Available

The Azure Functions team is thrilled to share that the v2 programming model for Python is now Generally Available!

 

The v2 programming model enables customers to easily create Functions applications – leaning towards fewer Functions concepts and instead emphasizing Python principles. Key improvements include triggers and bindings declared as decorators, a simplified folder structure, and easy to reference documentation.

 

Note that leveraging the v2 model will alter how you create functions, but the underlying experience regarding monitoring, debugging, and deployment will remain the same.

 

We are so grateful for the community support of the new model and the feedback we have gotten since we brought the experience to Preview last fall. We've iterated on usability and increased out-of-the-box support for numerous triggers and bindings. Furthermore, running existing Flask and Fast API apps on Azure Functions, and leveraging generic triggers and bindings are now easier than ever, enabling customers to integrate with first and third-party extensions seamlessly.

 

Comparing v1 & v2

 

Following is an example of a Function Application with an HTTP trigger using both the v1 and the v2 programming models:

 

File Name v1 v2
init.py import azure.functions def main(req: azure.functions.HttpRequest) -> str: user = req.params.get('user') return f'Hello, {user}!'  
function.json { "scriptFile": "__init__.py", "bindings": [ { "authLevel": "function", "type": "httpTrigger", "direction": "in", "name": "req", "methods": [ "get", "post" ] }, { "type": "http", "direction": "out", "name": "$return" } ] }  
function_app.py   import azure.functions app = func.FunctionApp() @app.function_name(name="HttpTrigger1") @app.route(route="req") def main(req: azure.functions.HttpRequest) -> str: user = req.params.get('user') return f'Hello, {user}!'


Note that using the v1 programming model, there would be an 'init.py' and 'function.json' file for each function within the function application. When using the v2 programming model, all functions can be defined within the 'function_app.py' file. This significantly decreases the number of files and simplifies the folder structure, making it easier to manage applications. Additionally, the v2 programming model includes support for blueprints, such that multiple Python files can contain functions.

 

Supported Triggers and Bindings

 

Following are the triggers and bindings supported with decorators today:

 

Type Trigger Input Binding Output Binding
HTTP x    
Timer x    
Azure Queue Storage x   x
Azure Service Bus topic x   x
Azure Service Bus queue x   x
Azure Cosmos DB x x x
Azure Blob Storage x x x
Azure Hub x   x

 

If you want to use an extension for which there isn't a decorator available, you can use the generic triggers and bindings. For example, you can use generic decorators for the new Azure Data Explorer extension!

 

Integrating Web Applications that use ASGI & WSGI Frameworks

 

Have an existing web application using Flask or Fast API? Running it on Functions is easier than ever - check out the following examples!

 

ASGI

 

# function_app.py import azure.functions as func from fastapi import FastAPI, Request, Response fast_app = FastAPI() @fast_app.get("/return_http_no_body") async def return_http_no_body(): return Response(content='', media_type="text/plain") app = func.AsgiFunctionApp(app=fast_app, http_auth_level=func.AuthLevel.ANONYMOUS)

 

 

WSGI

 

# function_app.py import azure.functions as func from flask import Flask, request, Response, redirect, url_for flask_app = Flask(__name__) logger = logging.getLogger("my-function") @flask_app.get("/return_http") def return_http(): return Response('<h1>Hello World™</h1>', mimetype='text/html') app = func.WsgiFunctionApp(app=flask_app.wsgi_app, http_auth_level=func.AuthLevel.ANONYMOUS)

 

 

Finally, modify the host.json file to include the code:

 

"extensions": { "http": { "routePrefix": "" } }

 

 

As you can see, just by adding a few lines of code, you can get your existing web applications running on Functions. Learn more here and try it out today!

 

Troubleshooting & What's Next

 

We are continuing to iterate and improve the v2 programming model experience for our customers. At the time of release, there are still deployments in progress until all features are available. Learn about current caveats and upcoming features here.

 

Thank you to our community for the support and feedback on this programming model. Please continue to let us know what you think, and report any issues you face. Happy coding!

Published on:

Learn more
Azure Compute Blog articles
Azure Compute Blog articles

Azure Compute Blog articles

Share post:

Related posts

Upcoming Changes to Instance Size Flexibility Ratios for Reserved VM Instances for M-series: What Yo

Overview In the ever-evolving landscape of cloud computing, it is crucial to stay informed about changes that may affect your usage and busine...

1 year ago

Exploring SUSE Enterprise Linux on Azure

Exploring SUSE Enterprise Linux on Azure In today's cloud-centric world, leveraging robust and reliable operating systems is crucial for busin...

1 year ago

Announcing Public Preview of new attach/detach disks API for VMs/VMSS

We are excited to announce the public preview of a new API that will make attaching and detaching disks to a VM faster and easier. The new API...

1 year ago

Fine-tuning a Hugging Face Diffusion Model on CycleCloud Workspace for Slurm

Introduction Azure CycleCloud Workspace for Slurm (CCWS) is a new Infrastructure as a Service (IaaS) solution which allows the users to purpos...

1 year ago

Public Preview Announcement-On Demand Capacity Reservation in Azure in China

Today, we're announcing the public preview of on demand capacity reservations for Azure Virtual Machines in Azure in China Cloud . Y...

1 year ago

Breaking change for Window Server 2022 Image Users with .NET 6

Azure Marketplace media images for Windows Server 2022 currently include .NET 6, but going forward will not include a .NET version with the im...

1 year ago

Announcing the public preview of the new Azure FXv2-series Virtual Machines

Today, Microsoft is announcing the public preview of the new Azure FXv2-series Virtual Machines (VMs), based on the 5th Generation Intel® Xeon...

1 year ago

Effortlessly Migrate Azure VMs between zones

For various reasons you might come across a situation when you need to migrate your Azure VMs from one zone to another. Migrating Azure VMs be...

1 year ago

Announcing Public Preview of Instance Mix on Virtual Machine Scale Sets

Today, we’re excited to announce that the ability to specify multiple different VM sizes in your Virtual Machine Scale Sets (VMSS) with Flexib...

1 year ago

Announcing General Availability of Attach & Detach of Virtual Machines on Virtual Machine Scale Sets

Today, we’re thrilled to announce that the ability to attach or detach Virtual Machines (VMs) to and from a Virtual Machine Scale Set (VMSS) w...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.