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

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

问题描述

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

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).

这就是我尝试过的.

  1. 创建一个新的Function App-我给它命名,并将其他所有设置保留为默认设置.

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

使用以下代码创建了Python HttpTrigger函数:

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()    

运行此函数将得到预期的"2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]"输出,因为2.7.8是Azure Functions环境中安装的默认python版本.

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. 然后按照此Azure Wiki页面

当我再次运行该函数时,我得到的输出是"3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]"-一切都很好.

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 -> "
}

日志显示:

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一起运行

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

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

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

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?我需要怎么做才能使其正常工作?

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 :

但是,作为一种解决方法,一切都在此处进行解释(作者所有功劳: Murat Eken )

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

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

  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.转到平台功能>所有设置>扩展> +添加

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

2.2.安装"Python 3.6.2 x86"扩展.

2.2. Install the "Python 3.6.2 x86" extension.

2.3.转到平台功能>应用程序设置

2.3. Go to "Platform features > Applications Settings

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

2.4. Add an handler mapping:
extension : fastCgi
processor: D:\home\python362x86\python.exe
arguments: D:\home\python362x86\wfastcgi.py

2.5添加一个名为WEBSITE_USE_PLACEHOLDER的应用程序设置并将其值设置为0.这对于解决

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.保存您的应用设置.

2.6. Save your app settings.

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

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

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函数中使用Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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