在 Azure Functions 中使用 Python 3

Using Python 3 in Azure Functions

我的目标是获得一些 Python 3 代码 运行ning 作为 Azure 函数,但我无法使 Python 3 工作(我意识到 python Azure Functions 中的支持仍处于试验阶段)。

这是我试过的方法。

  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 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]" 的输出,因为 2.7.8 是安装在 Azure Functions 环境中的 python 默认版本。

  1. 然后我使用 Kudu 将 python 3.6.1 的可嵌入版本上传到 d:\site\tools,如 this Azure wiki page
  2. 中所述

当我再次 运行 函数时,我得到了输出 "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 文件,那么功能将再次开始工作,但是 运行s 与 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 成功 运行ning?我需要做什么才能使其正常工作?

目前有一个未解决的问题:

但作为解决方法,一切都在解释 here (All credits to the author: 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

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, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]"

您可以使用 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": {}
          // ...
        }]
        // ...
      }
    }
  ]
}