【Azure Function】示例运行 python durable function(model V2)

简介: 【Azure Function】示例运行 python durable function(model V2)

问题描述

参考官方文档(使用 Python 创建你的第一个持久函数:https://learn.microsoft.com/zh-cn/azure/azure-functions/durable/quickstart-python-vscode), 部署后,却出现“Failed to load function”错误。

在结合以上参考文档后,可以通过如下的步骤创建并运行 Python Durable Function(Model V2)。

 

检查步骤

第一 : 确保 requirements.txt 包含下面二行内容

azure-functions  

azure-functions-durable

 

第二: 打开 VS Code  的  Terminal 命令行,在Function目录下运行下面几行命令:

python -m pip install -r requirements.txt

python.exe -m pip install --upgrade pip

pip install azure-functions-durable

 

第三 : 在本地文件   local.setting.json  检查是否有AzureWebJobsFeatureFlags字段 , 同样云端的Function Application Settings 中,也必须有AzureWebJobsFeatureFlags参数

"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"

 

第四:  在本地测试运行, 最好连接到 真实的Azure Storage Account, 本地模拟的Storage 有时候不工作

"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxxxxx

 

第五 : 查看本地目录下, 确保有这几个 Durable Python V2 的核心文件

  • function_app.py
  • host.json
  • requirements.txt

 

第六:Python Model V2 Durable Function 示例代码

文件名 function_app.py

import azure.functions as func
import azure.durable_functions as df
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
# An HTTP-Triggered Function with a Durable Functions Client binding
@myApp.route(route="httproute")
@myApp.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
    #function_name = req.route_params.get('functionName')
    function_name = "hello_orchestrator"
    instance_id = await client.start_new(function_name)
    response = client.create_check_status_response(req, instance_id)
    return response
# Orchestrator
@myApp.orchestration_trigger(context_name="context")
def hello_orchestrator(context):
    result1 = yield context.call_activity("get_result", "Seattle")
    result2 = yield context.call_activity("get_result", "Tokyo")
    result3 = yield context.call_activity("get_result", "London")
    return [result1, result2, result3]
 
# Activity
@myApp.activity_trigger(input_name="city")
def get_result(city: str):
    return "Hello " + city

 

第七: 本地测试

http://localhost:7071/api/httproute 这个可以返回 statusQueryGetUri 的 url ,http://localhost:7071/runtime/webhooks/durabletask/instances/xxxxxxxxxxxxxxxxx?taskHub=TestHubName&connection=Storage&code=xxxxxxxxxxxxxxxx,然后通过上面url 就可以看到 activity 的 结果了。

 

 

参考资料

使用 Python 创建你的第一个持久函数:https://learn.microsoft.com/zh-cn/azure/azure-functions/durable/quickstart-python-vscode

 

相关文章
|
API C++
【Azure 环境】VS Code登录China Azure(Function)报错 An error occurred while signing in: invalid_request - AADSTS65002
An error occurred while signing in: invalid_request - AADSTS65002: Consent between first party application 'c27c220f-ce2f-4904-927d-333864217eeb' and first party resource '797f4846-ba00-4fd7-ba43-dac1f8f63013' must be configured via preauthorization - applications owned and operated by Microsoft mus
656 13
|
存储 安全 数据安全/隐私保护
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
553 20
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
395 1
|
JSON 数据格式
【Azure 环境】当一个Azure Function资源创建很久了,是否可以获取到它的创建时间呢?
用户在尝试获取 Azure Function App 的创建时间时发现,资源 JSON 中缺少 `createdTime` 字段,仅能查看到 `lastModifiedTime`。同时,活动日志和 Azure Resource Graph 查询也未能提供创建时间信息。经解答,Azure 并未为所有资源类型记录创建时间,建议在部署时将创建时间作为标签记录。若创建时间在 90 天内,可通过部署日志间接获取。
421 7
|
JavaScript 前端开发 机器人
【Azure Bot Service】在中国区Azure上部署机器人的 Python 版配置
本文介绍了在中国区Azure上使用Python SDK配置Azure Bot Service时遇到的问题及解决方案,涵盖参数设置与适配器配置,适用于希望在Azure中国区部署Python机器人的开发者。
392 9
|
API 开发工具 网络架构
【Azure Service Bus】使用Python SDK创建Service Bus Namespace资源(中国区)
本文介绍了如何使用Python SDK创建Azure Service Bus Namespace资源。首先,通过Microsoft Entra ID注册应用获取Client ID、Client Secret和Tenant ID,完成中国区Azure认证。接着,初始化ServiceBusManagementClient对象,并调用`begin_create_or_update`方法创建资源。
403 30
|
网络协议 API 网络安全
【Azure Function App】发现部分请求Function App遇见 403.72 报错(请求Body>100KB)
在调用Azure Function的HTTP Trigger时,发送POST请求偶尔出现403错误,且响应为空、Header信息少。经排查发现,当请求Body大于100KB时会触发403.72错误,原因是启用了“Client Certificate mode”为“Optional Interactive User”。解决方法是将该模式设置为“Ignore”。由于TLS重新协商机制限制,大请求体无法正常处理,导致此问题。
498 1
|
安全 Linux 开发工具
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
629 9
|
Python
[oeasy]python086方法_method_函数_function_区别
本文详细解析了Python中方法(method)与函数(function)的区别。通过回顾列表操作如`append`,以及随机模块的使用,介绍了方法作为类的成员需要通过实例调用的特点。对比内建函数如`print`和`input`,它们无需对象即可直接调用。总结指出方法需基于对象调用且包含`self`参数,而函数独立存在无需`self`。最后提供了学习资源链接,方便进一步探索。
482 17

热门文章

最新文章

推荐镜像

更多