gpt4 book ai didi

python - Azure Function 中的 http 和计时器触发器

转载 作者:行者123 更新时间:2023-12-02 07:44:16 25 4
gpt4 key购买 nike

通过复制项目文件夹,我可以在 Azure 函数中同时拥有计时器和 http 触发器。这样我就有了一个单独的 function.json ,我可以在其中为一个指定计时器触发器,为另一个指定 http 触发器,请参阅 src_httpsrc_timer > 如下:

folders

这绝对是不可取的,因为我正在复制我的代码。

问题:有没有一种方法可以在一个函数中同时拥有计时器和 http 触发器?

我读到this看起来这是不可能的,我希望我是错的。

最佳答案

编辑:请参阅 Folder Structure 上现在提供的一些官方文档和 Import behavior .

<小时/>

在java中你可以做这样的事情,因为它在生成的function.json中使用class-name.function-name作为“scriptFile” >:

    public class EhConsumerFunctions {
private void processEvent(String request, final ExecutionContext context) {
// process...
}

@FunctionName("HttpTriggerFunc")
public void httpTriggerFunc(
@HttpTrigger(name = "req", methods = {HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> req,
final ExecutionContext context) {
processEvent(req.getBody().get(), context);
}

@FunctionName("TimerTriggerFunc")
public void timerTriggerFunc(
@TimerTrigger(name = "timerRequest", schedule = "0 */5 * * * *") String timerRequest,
final ExecutionContext context) {
processEvent(timerRequest, context);
}

}

对于 python,它需要脚本名称并期望它有一个 main 和单独的 function.json。所以你必须有两个文件夹和两个脚本。但每个脚本都可以导入一个进行实际处理的通用业务逻辑模块。

类似于:

MyFunctionApp
|____ host.json
|____ business
| |____ logic.py
|____ http_trigger
| |____ __init__.py
| |____ function.json
|____ timer_trigger
|____ __init__.py
|____ function.json

http_trigger/__init__.py 将具有:

from business import logic

def main(req: func.HttpRequest) -> func.HttpResponse:
return logic.process(req)

http_trigger/function.json 将具有:

    {
"scriptFile": "http_trigger/__init__.py",
"disabled": false,
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}

关于python - Azure Function 中的 http 和计时器触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65090379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com