You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入azure-eventhub致Linux环境Azure Function部署后无法运行

问题

将Python编写的Azure Function以Zip包形式部署到Linux环境的函数应用中,即使requirements.txt已包含azure-eventhub包,一旦代码中导入该包,部署后的函数就无法运行。本地运行完全正常,移除导入后函数应用中的代码也能正常运行。

复现步骤

  1. 创建包含以下文件的Zip包function_app.zip:
    • function_app.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="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)"
        }
      }
      
  2. 通过Azure CLI创建函数应用并部署:
    $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"
    az functionapp deployment source config-zip -g "$prefix-rg" -n "$prefix-function-app" --src .\function_app.zip
    
  3. 添加azure-eventhub的导入语句后重新部署,函数停止执行,日志显示1 functions found但0 functions loaded。

日志信息

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

解决方案

1. 指定兼容的azure-eventhub版本

Azure Functions v4的Python运行环境依赖特定版本的azure-core,最新版azure-eventhub可能要求更高版本的azure-core,导致依赖冲突。修改requirements.txt,指定兼容版本:

azure-functions
azure-eventhub==5.11.0

该版本与Azure Functions v4的Python运行环境兼容,不会引发依赖冲突。

2. 检查依赖安装日志

登录Azure Portal,进入函数应用的日志流或Deployment Center,查看部署过程中pip install的输出,确认是否有依赖安装失败、版本冲突的报错。如果有,根据报错调整依赖版本。

3. 使用远程构建部署

避免本地打包时的依赖环境差异,使用Azure的远程构建功能部署:

func azure functionapp publish $prefix-function-app --build remote

远程构建会在Azure的Python环境中安装依赖,确保与运行环境一致。

4. 调整Extension Bundle版本

尝试降低host.json中的Extension Bundle版本,避免与azure-eventhub包产生冲突:

"extensionBundle": {
  "id": "Microsoft.Azure.Functions.ExtensionBundle",
  "version": "[3.0.0, 3.15.0)"
}

本地验证

本地运行正常,步骤如下:

python -m venv .venv
.venv\scripts\activate
pip install -r requirements.txt
func init
# 启动azurite存储模拟器
azurite
# 启动函数
func start

内容的提问来源于stack exchange,提问作者gummersbach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 10:32:16