Azure Function Python V2 一个函数应用程序中的多个函数

问题描述 投票:0回答:1

我正在寻找有关在一个 Azure Function App 中为多个函数创建项目结构的指南。这是我之前读过的文章在一个 Azure Function App 中创建多个函数

有两件事我似乎找不到好的答案:

  1. 使用Visual Studio Code部署多个功能。
  2. 从上面的链接来看,我似乎必须先创建一个新函数,但是
    func azure functionapp publish <App Name>
    用于将应用程序发布到现有函数。

我尝试使用 VSCode 进行部署,但最终覆盖了另一个函数。

python azure azure-functions
1个回答
0
投票

为了一次部署多个函数,您需要执行一次

func init --worker-runtime python --model V2
命令,这将创建函数所需的所有默认文件。然后,如果您想在其中添加不同的触发功能,可以多次运行
func new
命令。

如果您将运行

func new
三次来创建一个 HTTP 触发函数和 2 个计时器触发函数,那么文件夹结构和
function_app.py
将如下所示 -

文件夹结构-

enter image description here

function_app.py-

import azure.functions as func
import datetime
import json
import logging

app = func.FunctionApp()

@app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def TimerTriggerFunction(myTimer: func.TimerRequest) -> None:
    
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

@app.route(route="HttpTriggeredFunction", auth_level=func.AuthLevel.ANONYMOUS)
def HttpTriggeredFunction(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}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

@app.timer_trigger(schedule="0 0 0/1 * * *", arg_name="myTimer", run_on_startup=True,
              use_monitor=False) 
def TimerTriggeredFunction2(myTimer: func.TimerRequest) -> None:
    
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')

您可以使用

func azure functionapp publish <function_APP_Name>
命令或 vs code 进行部署。

enter image description here

enter image description here

enter image description here

传送门-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.