您不能在 Azure Functions 之外使用 Python 编写代码吗?

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

我正在开发一个 Azure Function App,其中有多个定义为蓝图的 Azure Functions。

我想做的是创建 Azure 函数蓝图可以在与蓝图相同的文件内调用的函数(请参阅下面的示例)。

云端中断:

import logging
import azure.functions as func

bp = func.Blueprint()


@bp.timer_trigger(
    schedule="0 0 10 * * *",
    arg_name="myTimer",
    run_on_startup=False,
    use_monitor=False,
)
def my_azure_function(myTimer: func.TimerRequest) -> None:
    do_something()

# This breaks the Function App in the cloud, not locally.
def do_something():
    logging.info("test log")

当我尝试使用调试器运行它时,它在本地运行良好,但是当将其部署到azure时,该函数不会显示在函数应用程序概述下。

奇怪的是,删除该函数并在 Azure Function 蓝图本身下添加代码在部署时确实有效(请参阅下面的示例)。

不会在云端中断:

import logging
import azure.functions as func

bp = func.Blueprint()


@bp.timer_trigger(
    schedule="0 0 10 * * *",
    arg_name="myTimer",
    run_on_startup=False,
    use_monitor=False,
)
def my_azure_function(myTimer: func.TimerRequest) -> None:
    # This was done by a function before
    logging.info("test log")

这对我来说非常烦人,因为 Azure Function 需要做很多事情。我认为,将代码拆分为多个函数,然后可由 Azure 函数蓝图调用,这是解决此问题的最佳方法。

我已经做了一些研究,但无法找到解决此问题的方法。我浏览了 StackOverflow 问题并阅读了大部分 Azure Function 文档。

希望有人能告诉我如何解决这个问题或我做错了什么。

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

但是当部署到azure时,该函数不会显示在Function App概览下。

蓝图无法作为函数部署到 Azure Function。

您需要在主文件中导入蓝图。在 Azure 中,仅当使用

func.FunctionApp()
调用函数时才能识别函数。

作为参考,请检查此文档

我的目录:

blueprint.py
:

import azure.functions as func
import logging

bp = func.Blueprint()

@bp.timer_trigger(arg_name="mytimer", schedule="0 */5 * * * *", run_on_startup=False, use_monitor=False)
def my_azure_func(mytimer:func.TimerRequest):
    do_something()

def do_something():
    logging.info("test log")

function_app.py
:

import azure.functions as func
import logging
from blueprint import bp

app= func.FunctionApp()

app.register_functions(bp)

OUTPUT

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