python - 无法将数据推送到 Azure 事件中心

标签 python azure azure-eventhub

我正在尝试将数据示例数据推送到 Azure 事件中心,但我无法执行此操作。

import azure.functions as func
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
from azure.identity.aio import DefaultAzureCredential


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"Blob Size: {myblob.length} bytes")
    event_hub_connection_string = "Endpoint=sb://event-hub-namespace/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sharedaccesskey"
    event_hub_name = "event_hub_name"
    
    
    producer = EventHubProducerClient.from_connection_string(event_hub_connection_string, eventhub_name=event_hub_name)
    
    # event_data = "this is the first message"
    event_data = EventData(b'Hello, Event Hub!')
    with producer:
        producer.send_batch(event_data)

我收到以下错误,并且不确定我是否也传递了正确的连接字符串。

Result: Failure Exception: RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-1_0'. Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 479, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 752, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/event-hub-test/__init__.py", line 15, in main producer = EventHubProducerClient.from_connection_string(event_hub_connection_string, eventhub_name=event_hub_name) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/eventhub/aio/_producer_client_async.py", line 517, in from_connection_string return cls(**constructor_args) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/eventhub/aio/_producer_client_async.py", line 181, in __init__ ALL_PARTITIONS: self._create_producer() File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/eventhub/aio/_producer_client_async.py", line 354, in _create_producer handler = EventHubProducer( # type: ignore File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/eventhub/aio/_producer_async.py", line 110, in __init__ self._lock = asyncio.Lock(**self._internal_kwargs) File "/usr/local/lib/python3.9/asyncio/locks.py", line 81, in __init__ self._loop = events.get_event_loop() File "/usr/local/lib/python3.9/asyncio/events.py", line 642, in get_event_loop raise RuntimeError('There is no current event loop in thread %r.'

最佳答案

发生这种情况是因为导入引入了客户端的异步版本,并且 azure 函数正在同步运行。

请进行以下更改并尝试再次运行该函数

import azure.functions as func
from azure.eventhub import EventData
from azure.eventhub import EventHubProducerClient
from azure.identity import DefaultAzureCredential


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"Blob Size: {myblob.length} bytes")
    event_hub_connection_string = "Endpoint=sb://event-hub-namespace/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=sharedaccesskey"
    event_hub_name = "event_hub_name"
    
    
    producer = EventHubProducerClient.from_connection_string(event_hub_connection_string, eventhub_name=event_hub_name)
    
    # event_data = "this is the first message"
    event_data = EventData(b'Hello, Event Hub!')
    with producer:
        producer.send_batch(event_data)

您可以在我们的存储库 here 中找到更多示例

关于python - 无法将数据推送到 Azure 事件中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76670968/

相关文章:

python - Shutil.copy 不会复制

javascript - 如何在前端Javascript应用程序中订阅Azure SignalR服务?

azure - Eventhub Stream 未捕获架构不匹配

azure - 了解 Azure 事件中心分区使用者模式

python mysql多行插入失败

python - 在 PyStruct 中拟合 SSVM 模型时出现 IndexError

python - AIORedis 和 PUB/SUB 不是 asnyc

azure - 以编程方式安排 Azure 函数的一次性执行

c# - 依赖注入(inject)和 Mediatr 与 Azure Functions 结合使用是否效率低下?

azure - 是否可以将 eventhub 和服务总线添加到 Azure 中的资源组?