Azure Event Grid SDK for Python
使用 Azure Event Grid 发布和路由事件
通过此技能在 Azure 上构建事件驱动型应用程序。它将指导您使用 Azure Event Grid Python SDK 发布 CloudEvents 和 EventGridEvents。
下载技能 ZIP
在 Claude 中上传
前往 设置 → 功能 → 技能 → 上传技能
开启并开始使用
测试它
正在使用“Azure Event Grid SDK for Python”。 发布 CloudEvent 当订单创建时
预期结果:
以下是发布订单创建 CloudEvent 的代码:
from azure.eventgrid import EventGridPublisherClient, CloudEvent
from azure.identity import DefaultAzureCredential
client = EventGridPublisherClient(endpoint, DefaultAzureCredential())
event = CloudEvent(
type="MyApp.Events.OrderCreated",
source="/myapp/orders",
data={"order_id": "12345", "amount": 99.99}
)
client.send(event)
正在使用“Azure Event Grid SDK for Python”。 如何向 Azure Event Grid 进行身份验证?
预期结果:
使用 DefaultAzureCredential 进行身份验证:
1. 安装:pip install azure-identity azure-eventgrid
2. 设置环境变量:EVENTGRID_TOPIC_ENDPOINT
3. 创建客户端:EventGridPublisherClient(endpoint, DefaultAzureCredential())
DefaultAzureCredential 支持托管标识、服务主体和本地开发身份验证。
安全审计
安全This is a prompt-only skill containing documentation and code examples for the Azure Event Grid SDK for Python. Static analysis scanned 0 files with a risk score of 0/100. No security concerns identified as the skill provides reference documentation without any executable code or runtime behavior.
质量评分
你能构建什么
事件驱动微服务
构建通过 Event Grid 通信的解耦微服务
实时事件发布
从 Python 应用程序向 Azure Event Grid 发布 CloudEvents
无服务器集成
通过 Event Grid 事件连接 Azure Functions 和 Logic Apps
试试这些提示
帮助我在新订单创建时向 Azure Event Grid 发布 CloudEvent。事件应包含 order_id 和 amount 字段。
展示如何使用 Azure Event Grid Python SDK 通过批处理高效发布多个事件。
我需要为高吞吐量场景设置异步事件发布。指导我使用异步 EventGridPublisherClient。
解释如何配置和使用 Event Grid Namespaces 进行拉取交付。包括身份验证和主题配置。
最佳实践
- 对新应用程序使用 CloudEvents 格式,因为它是互操作性的行业标准
- 发布时将多个事件批量在一起以提高吞吐量并减少 API 调用
- 在事件中包含有意义的主旨以实现高效的过滤和路由
避免
- 不要在代码中硬编码连接字符串或凭据。使用环境变量或托管标识。
- 避免在高吞吐量场景中同步发送事件。改用异步客户端。
- 除非需要 Azure 原生功能,否则不要在新应用程序中使用 EventGridEvent 模式。优先使用 CloudEvents。