gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:12:00 28 4
gpt4 key购买 nike

我正在尝试将数据示例数据推送到 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/

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