June 20th, 2023

Announcing the stable release of the Service Bus library for Python using a pure Python AMQP stack

We’re excited to announce that the latest stable release of the Azure Service Bus library for Python now includes a new AMQP library written entirely in Python, removing the complexity of native interop. With significant contributions from the team–Anna Tisch, Kashif Khan, Swathi Pillalamarri, and Libba Lawrence–the library:

  • Is feature complete and available for download now, and
  • Follows our Azure SDK Guidelines, making for an idiomatic, consistent, approachable, diagnosable, and dependable library.

Library benefits

  • The Python-based library has improved stability, robustness, and maintenance, reducing any risks of C-related memory leaks or segmentation fault errors. Customers can more easily interact with and contribute to the source code.
  • The new library allows us to provide full multi-platform support, including ARM processors (Apple M1, etc.) and x86, without having to build separate wheels for individual platforms. The Service Bus library can now be integrated into a project like Home Assistant running on a Raspberry Pi.
  • The library was designed to be a drop-in replacement with no breaking changes, making the transition to this new version seamless for projects already using it.

Get started

Install the library

Install the Azure Service Bus client library with pip in your Python environment:

pip install azure-servicebus>=7.11.0

Create a Service Bus namespace and queue

For instructions on creating a Service Bus namespace and queue, follow this step-by-step guide.

Create the client and send a message to a queue

To create a client in this example, a Service Bus connection string and queue name are needed (obtained from the Azure portal):

from azure.servicebus import ServiceBusClient, ServiceBusMessage

connection_str = '<< CONNECTION STRING FOR THE SERVICE BUS NAMESPACE >>'
queue_name = '<< NAME OF THE QUEUE >>'
servicebus_client = ServiceBusClient.from_connection_string(conn_str=connection_str)

with servicebus_client:
    sender = servicebus_client.get_queue_sender(queue_name=queue_name)
    batch_message = sender.create_message_batch()
    messages_to_send = 10
    for i in range(messages_to_send):
        try:
            batch_message.add_message(ServiceBusMessage(f"Message inside a ServiceBusMessageBatch {i}"))
        except MesageSizeExceededError:
            # ServiceBusMessageBatch object reaches max_size, send the current batch
            # New ServiceBusMessageBatch object is created here to send more data.
            sender.send_messages(batch_message)
            batch_message = sender.create_message_batch()

    if len(batch_message)
        sender.send_messages(batch_message)

Create the client, receive & complete messages from a queue

from azure.servicebus import ServiceBusClient

connection_str = '<< CONNECTION STRING FOR THE SERVICE BUS NAMESPACE >>'
queue_name = '<< NAME OF THE QUEUE >>'
servicebus_client = ServiceBusClient.from_connection_string(conn_str=connection_str)

with servicebus_client:
    receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME)
    with receiver:
        received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=5)
        for msg in received_msgs:
            print(str(msg))
            receiver.complete_message(msg)

For more examples of how to use the library to send and receive events to/from Service Bus, see the GitHub samples directory or the Azure documentation samples. Information about backwards compatibility with the older library can be found in the README.

Summary

We encourage you to use this new version and we welcome your feedback!

For feature requests, bug reports, or general support, open an issue in the Azure SDK for Python repository.

Author

0 comments

Discussion are closed.