python - 在 Azure Functions 中使用 Python 3

标签 python python-3.x azure azure-functions

我的目标是让一些 Python 3 代码作为 Azure Function 运行,但我无法让 Python 3 工作(我意识到 Azure Functions 中的 python 支持仍处于实验阶段)。

这是我尝试过的。

  1. 创建一个新的 Function App - 我已为其指定了名称,并将其他所有内容保留为默认值。

  2. 使用以下代码创建了一个 Python HttpTrigger 函数:

    import sys
    import os
    
    response = open(os.environ['res'], 'w')
    response.write(sys.version)
    response.close()    
    

运行此函数会按预期输出 “2.7.8(默认,2014 年 6 月 30 日,16:03:49)[MSC v.1500 32 位(Intel)]”,因为2.7.8 是 Azure Functions 环境中安装的默认 Python 版本。

  • 然后,我使用 Kudu 将可嵌入版本的 python 3.6.1 上传到 d:\site\tools,如 this Azure wiki page 中所述。
  • 当我再次运行该函数时,我得到输出“3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]” - 所以一切都很好。

    但是,如果我重新启动函数应用程序(或者只是将其放置一段时间以使其关闭),那么一切都会崩溃。运行该函数给出:

    {
      "id": "dd7c4462-0d73-49e0-8779-67b15a9bba82",
      "requestId": "ff553805-501d-4ea6-93f6-7bd6fa445a37",
      "statusCode": 500,
      "errorCode": 0,
      "message": "Exception while executing function: Functions.HttpTriggerPython31 -> "
    }
    

    日志显示:

    2017-11-09T17:37:04.988 Function started (Id=941e5bef-e5e0-4604-8533-dd2a5fcaddf0)
    2017-11-09T17:37:05.348 Exception while executing function: Functions.HttpTriggerPython31. Microsoft.Azure.WebJobs.Script: .
    2017-11-09T17:37:05.364 Function completed (Failure, Id=941e5bef-e5e0-4604-8533-dd2a5fcaddf0, Duration=363ms)
    

    如果我从 d:\site\tools 中删除 python 文件,则函数将再次开始工作,但以 v2.7.8 运行

    但是,我可以从 Kudu 控制台运行 python 3.x:

    D:\home\site\tools>python -c "import sys;print(sys.version)"
    3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]
    

    还有其他人在 Azure Functions 中成功运行 Python 3 吗?我需要做什么才能使其正常工作?

    最佳答案

    目前有一个悬而未决的问题:

    但作为一种解决方法,一切都已解释 here (所有功劳归作者:Murat Eken)

    1. 创建您的函数。
    2. 使用替代 Python 版本安装站点扩展并配置处理程序映射以默认使用该安装。

      2.1。转到“平台功能 > 所有设置 > 扩展 > + 添加

      2.2。安装“Python 3.6.2 x86”扩展。

      2.3。转到“平台功能 > 应用程序设置”

      2.4。添加处理程序映射:
      扩展名:fastCgi
      处理器:D:\home\python362x86\python.exe
      参数:D:\home\python362x86\wfastcgi.py

    Azure Function - Handler mapping to use a specific python version

    2.5 添加名为 WEBSITE_USE_PLACEHOLDER 的应用程序设置并将其值设置为 0。这是解决 an Azure Functions issue that causes the Python extension to stop working after the function app is unloaded 问题所必需的。 .

    2.6。保存您的应用程序设置。

    这是我的函数的输出 “3.6.2(v3.6.2:5fd33b5,2017 年 7 月 8 日,04:14:34)[MSC v.1900 32 位(英特尔)]”

    您可以使用 ARM Template 自动执行这些步骤,这是模板中有趣的部分:

    {
      "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
      "contentVersion": "1.0.0.0",
      "parameters": {
        //...
      },
      "variables": {
        // ...
      },
      "resources": [
        // ...
        {
          "type": "Microsoft.Web/sites",
          // ...
          "kind": "functionapp",
          "properties": {
            // ...
            "siteConfig": {
              "handlerMappings": [{
                "arguments": "D:\\home\\python362x86\\wfastcgi.py",
                "extension": "fastCgi",
                "scriptProcessor": "D:\\home\\python362x86\\python.exe"
              }]
              // ...
            },
            "resources": [{
              "name": "python362x86",
              "type": "siteextensions",
              "apiVersion": "2016-08-01",
              "properties": {}
              // ...
            }]
            // ...
          }
        }
      ]
    }
    

    关于python - 在 Azure Functions 中使用 Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47208325/

    相关文章:

    python - 使用 Python 从 JSON API 获取特定值并插入到 Postgresql 中

    python-3.x - 如何在 Jupyter 笔记本输出单元格中禁用换行?

    azure - 二头肌 : How to concat tags

    python - 如何从 web.py 服务器应用程序中的 Post 获取 JSON 值?

    python - django-python 如何将文本转换为 pdf

    python - 绘制饼图时出现问题

    python - 绕过循环AttributeError : 'NoneType' object has no attribute 'findAll'

    python - Python3队列: When can a blocking wait with no timeout return None?

    azure - 如何在 Rust 中解码 Azure JWT token 以在我的应用程序中使用有效负载

    logging - 在 Azure 中配置文件日志记录以写入 LocalResources 中定义的特定 LocalStorage