官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 道真| 内黄县| 大同市| 阜城县| 冷水江市| 河北省| 溆浦县| 巴马| 祁门县| 边坝县| 兴安盟| 赤壁市| 当阳市| 赤水市| 台东县| 昔阳县| 五家渠市| 阜新市| 漳浦县| 怀化市| 五华县| 宣恩县| 瑞昌市| 长乐市| 溧水县| 房产| 扎赉特旗| 永德县| 水富县| 宁明县| 井研县| 阜康市| 达尔| 永兴县| 上犹县| 祁门县| 区。| 固原市| 社旗县| 抚顺市| 清水县|