在 Azure Functions 中使用 Python 3 [英] Using Python 3 in Azure Functions

查看:19
本文介绍了在 Azure Functions 中使用 Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我尝试过的.

  1. 创建一个新的函数应用程序 - 我给它起了一个名字,其他的都默认了.

  2. 创建了一个 Python HttpTrigger 函数,代码如下:

    导入系统导入操作系统响应 = 打开(os.environ['res'],'w')response.write(sys.version)响应关闭()

运行此函数会按预期输出 "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 ools,如

    2.5 添加一个名为 WEBSITE_USE_PLACEHOLDER 的应用程序设置并将其值设置为 0.这是解决 一个导致 Python 扩展在卸载函数应用后停止工作的 Azure Functions 问题.

    2.6.保存您的应用设置.

    这是我的函数的输出"3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]"

    您可以使用 ARM 模板,这里是模板中有趣的部分:

    <代码>{"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json","contentVersion": "1.0.0.0",参数": {//...},变量":{//...},资源": [//...{"type": "Microsoft.Web/sites",//...种类":功能应用程序",特性": {//...站点配置":{处理程序映射":[{"参数": "D:\home\python362x86\wfastcgi.py",扩展":fastCgi","scriptProcessor": "D:\home\python362x86\python.exe"}]//...},资源": [{名称":python362x86",类型":站点扩展","apiVersion": "2016-08-01",特性": {}//...}]//...}}]}

    I'm aiming to get some Python 3 code running as an Azure Function, but I can't get Python 3 to work (I realize python support in Azure Functions is still experimental).

    Here's what I've tried.

    1. Create a new Function App - I've given it a name, and left everything else as default.

    2. Created a Python HttpTrigger function, with the following code:

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

    Running this function gives an output of "2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]" as expected, because 2.7.8 is the default version of python that is installed in the Azure Functions environment.

    1. I then uploaded the embeddable version of python 3.6.1 to d:site ools using Kudu, as described in this Azure wiki page

    When I run the function again, I get the output "3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]" - so all is good there.

    However, if I restart the function app (or just leave it alone for a while so it gets shut down) then everything breaks. Running the function gives:

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

    The logs show:

    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)
    

    If I delete the python files from d:site ools, then then function starts working again, but runs with v2.7.8

    However, I can get python 3.x to run from the Kudu console:

    D:homesite	ools>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)]
    

    Does anyone else have Python 3 successfully running in Azure Functions? What do I need to do to get it working?

    解决方案

    For the moment there is an open issue :

    But as a workaround, everything is explain here (All credits to the author: Murat Eken)

    1. Create your function.
    2. Installing a site extension with an alternative Python version and configuring the handler mapping to use that installation by default..

      2.1. Go to "Platform features > All Settings > Extensions > + Add

      2.2. Install the "Python 3.6.2 x86" extension.

      2.3. Go to "Platform features > Applications Settings

      2.4. Add an handler mapping:
      extension : fastCgi
      processor: D:homepython362x86python.exe
      arguments: D:homepython362x86wfastcgi.py

    2.5 Add an application setting called WEBSITE_USE_PLACEHOLDER and set its value to 0. This is necessary to work around an Azure Functions issue that causes the Python extension to stop working after the function app is unloaded.

    2.6. Save your app settings.

    Here is the output of my function "3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)]"

    you can automate these step using an ARM Template, here are the interesting part of the 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": {}
              // ...
            }]
            // ...
          }
        }
      ]
    }
    

    这篇关于在 Azure Functions 中使用 Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆