I found this example with python, which sends messages to service bus topic, but this is very slow.
def send_messages():
servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)
async with servicebus_client:
sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)
async with sender:
await send_single_message(sender)
Every single example, when sends messages creates servicebus client and topic sender for every message. Is it possible to reuse topic sender? Creating topic sender is very slow. I create messages from iterator, I cannot batch them, I have to send messages one by one, but with this solution sending 100 messages takes over 120 seconds :/
I tried sync solution, with reusing topic sender and sending 100 messages takes only 5 seconds.
Ideally, I want to find solution to inject topic sender with dependency injector library.
Creating a new service bus client and topic sender for every single message is not an optimal approach, and it can indeed lead to slower message-sending performance due to the overhead of creating and tearing down the connections for each message. To achieve better performance, you should create the service bus client and topic sender once and then reuse them for sending multiple messages. Yes, it is possible and recommended to reuse the topic sender when sending multiple messages to a Service Bus topic.
Sending 100 messages to Azure Service Bus should not take long if the messages are relatively small and the network connection is stable. However, if you want to introduce some delay between sending each message to simulate a real-world scenario or to avoid rate-limiting issues, you can use
asyncio.sleep().