{"id":2717,"date":"2023-06-20T08:41:10","date_gmt":"2023-06-20T15:41:10","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=2717"},"modified":"2023-06-20T08:41:10","modified_gmt":"2023-06-20T15:41:10","slug":"stable-service-bus-in-pure-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/stable-service-bus-in-pure-python\/","title":{"rendered":"Announcing the stable release of the Service Bus library for Python using a pure Python AMQP stack"},"content":{"rendered":"<p>We&#8217;re excited to announce that the latest stable release of the <a href=\"https:\/\/pypi.org\/project\/azure-servicebus\/5.11.0\/\">Azure Service Bus library for Python<\/a> now includes a new AMQP library written entirely in Python, removing the complexity of native interop. With significant contributions from the team&#8211;<a href=\"https:\/\/github.com\/annatisch\">Anna Tisch<\/a>, <a href=\"http:\/\/github.com\/kashifkhan\">Kashif Khan<\/a>, <a href=\"https:\/\/github.com\/swathipil\">Swathi Pillalamarri<\/a>, and <a href=\"https:\/\/github.com\/l0lawrence\">Libba Lawrence<\/a>&#8211;the library:<\/p>\n<ul>\n<li>Is feature complete and available for download now, and<\/li>\n<li>Follows our <a href=\"https:\/\/aka.ms\/azsdk\/guide\">Azure SDK Guidelines<\/a>, making for an idiomatic, consistent, approachable, diagnosable, and dependable library.<\/li>\n<\/ul>\n<h2>Library benefits<\/h2>\n<ul>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<\/ul>\n<h2>Get started<\/h2>\n<h3>Install the library<\/h3>\n<p>Install the Azure Service Bus client library with <code>pip<\/code> in your Python environment:<\/p>\n<pre><code class=\"language-python\">pip install azure-servicebus&gt;=7.11.0<\/code><\/pre>\n<h3>Create a Service Bus namespace and queue<\/h3>\n<p>For instructions on creating a Service Bus namespace and queue, follow this <a href=\"https:\/\/learn.microsoft.com\/azure\/service-bus-messaging\/service-bus-quickstart-portal\">step-by-step guide<\/a>.<\/p>\n<h3>Create the client and send a message to a queue<\/h3>\n<p>To create a client in this example, a Service Bus connection string and queue name are needed (obtained from the <a href=\"https:\/\/learn.microsoft.com\/azure\/service-bus-messaging\/service-bus-python-how-to-use-topics-subscriptions?tabs=connection-string\">Azure portal<\/a>):<\/p>\n<pre><code class=\"language-python\">from azure.servicebus import ServiceBusClient, ServiceBusMessage\r\n\r\nconnection_str = '&lt;&lt; CONNECTION STRING FOR THE SERVICE BUS NAMESPACE &gt;&gt;'\r\nqueue_name = '&lt;&lt; NAME OF THE QUEUE &gt;&gt;'\r\nservicebus_client = ServiceBusClient.from_connection_string(conn_str=connection_str)\r\n\r\nwith servicebus_client:\r\n    sender = servicebus_client.get_queue_sender(queue_name=queue_name)\r\n    batch_message = sender.create_message_batch()\r\n    messages_to_send = 10\r\n    for i in range(messages_to_send):\r\n        try:\r\n            batch_message.add_message(ServiceBusMessage(f\"Message inside a ServiceBusMessageBatch {i}\"))\r\n        except MesageSizeExceededError:\r\n            # ServiceBusMessageBatch object reaches max_size, send the current batch\r\n            # New ServiceBusMessageBatch object is created here to send more data.\r\n            sender.send_messages(batch_message)\r\n            batch_message = sender.create_message_batch()\r\n\r\n    if len(batch_message)\r\n        sender.send_messages(batch_message)<\/code><\/pre>\n<h3>Create the client, receive &amp; complete messages from a queue<\/h3>\n<pre><code class=\"language-python\">from azure.servicebus import ServiceBusClient\r\n\r\nconnection_str = '&lt;&lt; CONNECTION STRING FOR THE SERVICE BUS NAMESPACE &gt;&gt;'\r\nqueue_name = '&lt;&lt; NAME OF THE QUEUE &gt;&gt;'\r\nservicebus_client = ServiceBusClient.from_connection_string(conn_str=connection_str)\r\n\r\nwith servicebus_client:\r\n    receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME)\r\n    with receiver:\r\n        received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=5)\r\n        for msg in received_msgs:\r\n            print(str(msg))\r\n            receiver.complete_message(msg)<\/code><\/pre>\n<p>For more examples of how to use the library to send and receive events to\/from Service Bus, see the <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-python\/blob\/main\/sdk\/servicebus\/azure-servicebus\/samples\">GitHub samples<\/a> directory or the <a href=\"https:\/\/learn.microsoft.com\/azure\/service-bus-messaging\/service-bus-python-how-to-use-queues\">Azure documentation samples<\/a>. Information about backwards compatibility with the older library can be found in the <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-python\/tree\/main\/sdk\/servicebus\/azure-servicebus#pure-python-amqp-transport-and-backward-compatibility-support\">README<\/a>.<\/p>\n<h2>Summary<\/h2>\n<p>We encourage you to use this new version and we welcome your feedback!<\/p>\n<p>For feature requests, bug reports, or general support, <a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-python\/issues\">open an issue<\/a> in the Azure SDK for Python repository.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post announces the stable release of the Python library for Service Bus, which uses a new AMQP library written purely in Python.<\/p>\n","protected":false},"author":21139,"featured_media":2722,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[823,162,779],"class_list":["post-2717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-amqp","tag-python","tag-service-bus"],"acf":[],"blog_post_summary":"<p>This post announces the stable release of the Python library for Service Bus, which uses a new AMQP library written purely in Python.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/21139"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=2717"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2717\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/2722"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=2717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=2717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=2717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}