gpt4 book ai didi

python - azure-eventhub 导入破坏了 Azure 功能

转载 作者:行者123 更新时间:2023-12-03 00:17:38 25 4
gpt4 key购买 nike

问题

我正在将 python azure 函数作为 zip 包部署到 Linux 函数应用程序。
如果我的代码包含 azure-eventhub 包的导入,则该函数在部署后不会运行,即使 azure-eventhub 包包含在 需求中.txt.
但它确实在本地运行,没有任何问题。
如果删除导入,它也可以在函数应用程序中正常运行。

重现

创建包含以下文件的 zip 文件 function_app.zip:
function_app.py:

import logging
import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="source")
@app.schedule(
arg_name="timer",
schedule="*/10 * * * * *",
run_on_startup=True,
use_monitor=False
)
def source(timer: func.TimerRequest) -> None:
logging.info("Source executed!")

requirements.txt:

azure-functions
azure-eventhub

host.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.15.0, 4.0.0)"
}
}

从 Azure CLI 创建 Azure 函数应用:

$prefix = "yourprefix"
az login
az group create --name "$prefix-rg" --location "East US"
az storage account create --name "${prefix}storage" --location "East US" --resource-group "$prefix-rg" --sku "Standard_LRS"
az functionapp create --name "$prefix-function-app" --storage-account "${prefix}storage" --consumption-plan-location "eastus" --resource-group "$prefix-rg" --functions-version 4 --os-type "Linux" --runtime "python"

将 zip 文件部署到函数应用:

az functionapp deployment source config-zip -g "$prefix-rg" -n "$prefix-function-app" --src .\function_app.zip

代码现在已部署到函数应用程序,并且 source 函数每十秒执行一次,您可以通过查看日志流来确认这一点。但是,当您添加

from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient

导入到 zip 文件中的 function_app.py 文件并使用以下命令重新部署该函数

az functionapp deployment source config-zip -g "$prefix-rg" -n "$prefix-function-app" --src .\function_app.zip

,就不会再执行了。

(运行 az group delete --name "$prefix-rg" 来清理资源。)

日志

部署后函数应用的日志流:

Connected!
2023-03-23T13:31:59Z [Information] Loading functions metadata
2023-03-23T13:31:59Z [Information] Reading functions metadata
2023-03-23T13:31:59Z [Information] 1 functions found
2023-03-23T13:31:59Z [Information] 0 functions loaded
2023-03-23T13:31:59Z [Information] Loading functions metadata
2023-03-23T13:31:59Z [Information] Reading functions metadata
2023-03-23T13:31:59Z [Information] 1 functions found
2023-03-23T13:31:59Z [Information] 0 functions loaded
2023-03-23T13:33:52Z [Information] Host lock lease acquired by instance ID '00000000000000000000000009A7A025'.
2023-03-23T13:35:46Z [Information] Host lock lease acquired by instance ID '00000000000000000000000060EAB21B'.
2023-03-23T13:46:14Z [Verbose] Received request to drain the host
2023-03-23T13:46:14Z [Information] DrainMode mode enabled
2023-03-23T13:46:14Z [Information] Calling StopAsync on the registered listeners
2023-03-23T13:46:14Z [Information] Call to StopAsync complete, registered listeners are now stopped

本地运行函数

如果您想确认该函数是否通过导入在本地运行,请按照以下步骤操作:

运行

python -m venv .venv
.venv\scripts\activate
pip install -r requirements.txt

然后运行:

func init

启动azurite存储模拟器在单独的进程中。
您可以使用 npm install -g azurite 安装 azurite。运行蓝铜矿

azurite

您现在可以运行该函数。
运行:

func start

即使有导入,它也会每十秒运行一次。

最佳答案

根据 MSDoc ,v2 编程模型当前不支持多个 Python 工作线程设置。使用 v2 模型开发的函数不支持通过将 FUNCTIONS WORKER PROCESS COUNT 设置为大于 (>1) 1 来启用智能并发。

我建议您尝试在 host.json 文件中将 FUNCTIONS_WORKER_PROCESS_COUNT 设置为 1。

如果问题仍然存在,请在单独的模块中使用 event-hub 包,并将其导入到函数中,如上述文档中详述。

注意:并卸载 event-hub 软件包并使用最新版本 (5.11.2) 重新安装。

pip install azure-eventhub

检查完上述内容后,我在我的环境中尝试了与您相同的要求,并成功部署和执行。

init.py:

import  logging
import azure.functions as func
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
app = func.FunctionApp()
@app.function_name(name="")
@app.schedule(
arg_name="",
schedule="*/5 * * * * *",
run_on_startup=True,
use_monitor=False
)
def source(timer: func.TimerRequest) -> None:
logging.info("")

更新的代码:

host.json:

{  "version": "2.0",
"extensions": {
"eventhubs": {
}
},
"FUNCTIONS_WORKER_PROCESS_COUNT": <number_of_worker_processes>
}

enter image description here

enter image description here

关于python - azure-eventhub 导入破坏了 Azure 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75823906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com