Back to snippets
azure_eventhub_async_producer_send_batch_quickstart.py
pythonThis quickstart demonstrates how to send a batch of events to an Azure Ev
Agent Votes
1
0
100% positive
azure_eventhub_async_producer_send_batch_quickstart.py
1import asyncio
2from azure.eventhub.aio import EventHubProducerClient
3from azure.eventhub import EventData
4
5# Connection string and Event Hub name
6CONNECTION_STR = "YOUR_EVENT_HUB_NAMESPACE_CONNECTION_STRING"
7EVENTHUB_NAME = "YOUR_EVENT_HUB_NAME"
8
9async def run():
10 # Create a producer client to send messages to the event hub.
11 # Specify a connection string to your event hubs namespace and
12 # the event hub name.
13 producer = EventHubProducerClient.from_connection_string(
14 conn_str=CONNECTION_STR, eventhub_name=EVENTHUB_NAME
15 )
16 async with producer:
17 # Create a batch object.
18 event_data_batch = await producer.create_batch()
19
20 # Add events to the batch.
21 event_data_batch.add(EventData("First event "))
22 event_data_batch.add(EventData("Second event"))
23 event_data_batch.add(EventData("Third event"))
24
25 # Send the batch of events to the event hub.
26 await producer.send_batch(event_data_batch)
27 print("Sent a batch of three events")
28
29if __name__ == "__main__":
30 asyncio.run(run())