Mastering Azure Serverless Computing
上QQ阅读APP看书,第一时间看更新

Azure Functions and Python

If you want to start writing Azure Functions in Python you need to install Python version 3.6.x (you can download the installation package at https://www.python.org/downloads/; make sure that the minor version is 3.6). If you want to use VS Code to implement your Azure Functions in Python then you also need the Python extension for VS Code (you can download it directly inside VS Code or by going to https://marketplace.visualstudio.com/items?itemName=ms-python.python).

Once you have installed Python on your machine, you can create your first Azure Function in the same way you did for C# and Node.js.

First of all, you have to create a virtual environment. You can do this opening Command Prompt and using the following commands:

py -3.6 -m venv .env
.env\scripts\activate

In this case, you create a virtual environment called .env and activate it. Once you are in the virtual environment, you can initialize your Azure Function project:

(.env) C:\MasteringServerless\MyFirstPythonFunction>func init --worker-runtime python

The following screenshot shows the output of the preceding command:

Finally, you can create the Azure Function:

(.env) C:\MasteringServerless\MyFirstPythonFunction>func new --name HttpPythonFunction --template HttpTrigger

The following screenshot shows the output of the preceding command:

The folder structure for a Python function project is shown in the following diagram:

You have a folder for each function in your project and a SharedCode folder (if you need it) to contain code that is shared by different functions.

The __init__.py file contains the following function code:

import logging
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

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}!")
else:
return func.HttpResponse("Please pass a name on the query string or in the request body", status_code=400)

A function in Python should be a stateless method that gets inputs, processes them, and produces output. By default, the Azure Functions Runtime looks for a method called main() in the __init__.py file. If you need or want to, you can change this convention using the scriptFile and entryPoint properties in the function.json file:

{
"scriptFile": "myScript.py",
"entryPoint": "myentry",
...
}

The bindings, as usual, are defined in the function.json file:

{
...,
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

In the Python function, you can write the log using the logging handler, which exposes a set of methods, such as critical(), error(), and warning().

Once you have created your function, you can start and host it:

(.env) C:\MasteringServerless\MyFirstPythonFunction>func host start

The following screenshot shows the output of the preceding command:

Also, in this case (in the same way you saw for the Node.js), the runtime initializes the host reading the host.json file, then starts it and starts the language worker process to support Python, as you can see in the following screenshot:

Finally, the runtime starts one job for each function discovered in the function app folder, and your functions are up and running.

More information about the language worker and language extensibility will be given in later sections.