python - HttpTrigger 使用python依次执行

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

我使用 python 创建了一个 HttpTrigger Azure 函数。但我想在同一项目中使用 python 创建另一个 HttpTrigger Azure Functions。在此应用程序中,我想在执行第二个 HttpTrigger Azure 函数之后执行第一个 HttpTrigger Azure 函数。我怎样才能实现它?

因为在 Python Azure Functions 中没有持久函数。这就是为什么我无法理解如何执行一个又一个的 azure 函数。

最佳答案

假设有两个HttpTrigger函数HttpTriggerAHttpTriggerB ,我想到的直接解决方案是请求HttpTriggerA的公共(public)url来自HttpTriggerB通过使用 requests 的 HTTP 客户端请求来实现功能,如我的第一个版本 HttpTriggerB 中的代码所示代码。

import logging

import azure.functions as func
import requests
from urllib.parse import urlparse

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name') or 'Peter Pan'
    codeA = req.params.get('codeA') or ''
    o = urlparse(req.url)
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        print("B be invoked.")
        resp = requests.get(f"{o.scheme}://{o.netloc}/api/HttpTriggerA?name={name}&code={codeA}")
        return func.HttpResponse(f"Hello {name}! from B {resp.status_code == 200 and resp.text or ''}")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

但是,我在本地或Azure上测试上面的代码后,它不起作用。它将卡在代码行 resp = requests.get(f"{o.scheme}://{o.netloc}/api/HttpTriggerA?name={name}&code={codeA}") 处,当我访问 url http(s)://<the host of local or Azure>:<7071 or 80>/api/HttpTriggerB?name=Peter%20Pan&code=<code for HttpTriggerB>&codeA=<code for HttpTriggerA> 时。挂起问题的原因似乎是由在单例或单线程中运行的函数引起的。

所以我切换到另一个解决方案,从HttpTriggerB的html内容中使用Ajax请求。它的工作原理如下图所示。

enter image description here

这是我的代码 HttpTriggerB函数,HttpTriggerA函数只需由 func new 生成.

import logging

import azure.functions as func
#import requests
from urllib.parse import urlparse

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name') or 'Peter Pan'
    codeA = req.params.get('codeA') or ''
    o = urlparse(req.url)
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        print("B be invoked.")
        #resp = requests.get(f"{o.scheme}://{o.netloc}/api/httptriggera?name={name}&code={req.params.get('codeA')}")
        html = """
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

Hello """+name+"""! from B<br/>
<span id="A"></span>

<script>
$.when( 
  $.get('"""+o.scheme+'://'+o.netloc+'/api/HttpTriggerA?name='+name+'&code='+codeA+"""'),
  $.ready
).done(function( data ) {
  $( "#A" ).html( data[0] );
});
</script>
"""
        return func.HttpResponse(html, mimetype="text/html")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

希望有帮助。

关于python - HttpTrigger 使用python依次执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143531/

相关文章:

python - 覆盖 Debian/Ubuntu lintian 配置文件

python-3.x - 使用 tkinter.filedialog 时为 "libpng warning: iCCP: known incorrect sRGB profile"

python-3.x - 如何使用唯一的文件名python重命名文件

python - 登录Python?

python - 在 Python/Pyspark 中获取每月计数的更有效方法

python - 安装 functools 给我 AttributeError 'module' object has no attribute 'compose'

python - 执行练习代码时,即使在 datetime 模块之后也会出现错误

azure - Azure 事件中心的 Web/桌面客户端

c# - Azure 自定义脚本停留在转换状态

asp.net - ASP.NET Web API 的服务总线架构