繁体   English   中英

如何使用 EventHubConsumerClient class 运行异步 Python 事件中心触发器 Azure Function?

[英]How to run an async Python Event-Hub trigger Azure Function using EventHubConsumerClient class?

我正在尝试开发一个事件中心触发器 azure function,它可以从第一个事件中心接收事件并将这些事件发送到第二个事件中心。 作为附加功能,我希望我的 function 是异步的,并在 Azure Blob Storage 中使用存储检查点 为此,我想使用azure-eventhub 库的EventHubConsumerClient class( https: //pypi.org/project/azure-eventhub/,https://learn.microsoft.com/en-us/javascript/api /@azure/event-hubs/eventhubconsumerclient?view=azure-node-latest )

但是,当我在 VSCode 上本地测试 function 时,我似乎无法首先收到事件。

我正在侦听的事件中心有两个分区。 它的共享访问策略设置为发送和监听。 我有一个小脚本可以向他发送消息进行测试,效果很好。 我的 Azure function 运行时是 4.x 和 python 3.9.13,本地有一个 conda 基础。

这是我的 function 的代码,用于在我的init .py 中使用 EventHubConsumerClient class 接收事件:

import logging
import asyncio
import os
from azure.eventhub.aio import EventHubConsumerClient
from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore
import azure.functions as func

CONNECTION_STR = os.environ.get("EVENT_HUB_CONN_STR")
EVENTHUB_NAME = os.environ.get("EVENT_HUB_NAME")
STORAGE_CONNECTION_STR = os.environ.get("AZURE_STORAGE_CONN_STR")
BLOB_CONTAINER_NAME = os.environ.get("AZURE_STORAGE_NAME")


async def on_event(partition_context, event):
    logging.info("Received event with body: {} from partition: {}.".format(event.body_as_str(encoding="UTF-8"), partition_context.partition_id))
    await partition_context.update_checkpoint(event)


async def receive(client):
    await client.receive(
        on_event=on_event,
        starting_position="-1",  # "-1" is from the beginning of the partition.
    )


async def main(one_event: func.EventHubEvent):
    checkpoint_store = BlobCheckpointStore.from_connection_string(STORAGE_CONNECTION_STR, BLOB_CONTAINER_NAME)
    client = EventHubConsumerClient.from_connection_string(
        CONNECTION_STR,
        consumer_group="$Default",
        eventhub_name=EVENTHUB_NAME,
        checkpoint_store=checkpoint_store,
    )
    async with client:
        await receive(client)


if __name__ == '__main__':
    asyncio.run(main())

来源: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/eventhub/azure-eventhub/samples/async_samples/recv_with_checkpoint_store_async.py

注意:我知道 main 中的 one_event 没有在主代码中使用,但我希望他充当运行 main 的触发器。

我的 function.json 文件是:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "one_event",
      "direction": "in",
      "eventHubName": "<My_event_hub_name>",
      "connection": "<My_event_hub_co_str>",
      "cardinality": "one",
      "consumerGroup": "$Default"
    }
  ]
}

我在其中定义了一个事件中心输入绑定以用作触发器。

我还有一个 local.settings.json,其中包含一些变量和 requirements.txt,它似乎不缺少任何库。

仅供参考:我测试了另一种方法(此处: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=in-process%2Cfunctionsv2%2Cextensionv5&pivots= programming-language-python )(不使用 EventHubConsumerClient 类)来接收事件并且它工作正常但我没有检查点和异步功能。

在使用“func start”在本地运行 function 后,我没有接收和打印有关接收到的事件的一些基本信息,而是在我的终端中不断打印了很多消息。 它继续打印并锁定我的终端,所以我必须手动杀死它并创建一个新的。

所以看起来我的代码不能正常工作。

*我可能搞砸了 main() 和 asyncio.run() 方法。 * 你有没有机会知道问题可能是什么? 非常感谢你!

我不是 Python 专家,但在概念层面上我可以说当使用常规事件中心触发器时, 检查点仍然会发生,使用存储帐户

AzureWeb 作业存储

Azure Functions 运行时使用此存储帐户连接字符串进行正常操作。 此存储帐户的一些用途包括密钥管理、计时器触发器管理和事件中心检查点。 存储帐户必须是支持 blob、队列和表的通用帐户

在引擎盖下,触发器使用EventProcessorHost ,它类似于EventHubConsumerClient (我想 azure function 运行时将很快更新为也使用 EventHubConsumerClient)。

所以,我不确定你想要达到什么目的。 您似乎已将事件中心触发 function 与您自己的事件中心侦听器组合在一起。 您正在使用的EventHubConsumerClient将等待新的事件中心消息到达并阻止进一步执行,直到明确停止。 这不适用于 azure function,执行时间应该很短,默认情况下限制为 5 分钟。 例如,如果您要连续运行 azure web 作业,则使用EventHubConsumerClient是有意义的。

我正在尝试开发一个事件中心触发器 azure function,它可以从第一个事件中心接收事件并将这些事件发送到第二个事件中心。

我会说你需要一个事件中心触发 function事件中心 output 绑定将消息从一个事件中心传递到另一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM