How to use Service Bus queues with Python
This article describes how to use Service Bus queues. The samples are written in Python and use the Python Azure Service Bus package. The scenarios covered include creating queues, sending and receiving messages, and deleting queues.
What are Service Bus queues?
Service Bus queues support a brokered messaging communication model. When using queues, components of a distributed application do not communicate directly with each other; instead they exchange messages via a queue, which acts as an intermediary (broker)...
To begin using Service Bus messaging entities in Azure, you must first create a namespace with a name that is unique across Azure. A namespace provides a scoping container for addressing Service Bus resources within your application.
To create a namespace:
- Log on to the Azure portal.
- In the left navigation pane of the portal, click + Create a resource,
Creating a new namespace automatically generates an initial Shared Access Signature (SAS) rule with an associated pair of primary and secondary keys that each grant full control over all aspects of the namespace. See Service Bus authentication and authorization for information about how to create further rules with more constrained rights for regular senders and receivers. To copy the initial rule, Connection string–primary key, to copy the connection string to your clipboard for later use. Paste this value into Notepad or some other temporary location.
Repeat the previous step, copying and pasting the value of Primary key to a temporary location for later use.
Note
To install Python or the Python Azure Service Bus package, see the Python Installation Guide.
Create a queue
The ServiceBusService object enables you to work with queues. Add the following code near the top of any Python file in which you wish to programmatically access Service Bus:
from azure.servicebus import ServiceBusService, Message, Queue
The following code creates a ServiceBusService object. Replace
mynamespace,
sharedaccesskeyname, and
sharedaccesskey with your namespace, shared access signature (SAS) key name, and value.
bus_service = ServiceBusService( service_namespace='mynamespace', shared_access_key_name='sharedaccesskeyname', shared_access_key_value='sharedaccesskey')
The values for the SAS key name and value can be found in the Azure portal connection information, or in the Visual Studio Properties pane when selecting the Service Bus namespace in Server Explorer (as shown in the previous section).
bus_service.create_queue('taskqueue')
The
create_queue method also supports additional options, which enable you to override default queue settings such as message time to live (TTL) or maximum queue size. The following example sets the maximum queue size to 5 GB, and the TTL value to 1 minute:
queue_options = Queue() queue_options.max_size_in_megabytes = '5120' queue_options.default_message_time_to_live = 'PT1M' bus_service.create_queue('taskqueue', queue_options)
Send messages to a queue
To send a message to a Service Bus queue, your application calls the
send_queue_message method on the ServiceBusService object.
The following example demonstrates how to send a test message to the queue named
taskqueue using
send_queue_message:
msg = Message(b'Test Message') bus_service.send_queue_message('taskqueue', msg)
Service Bus queues support a maximum message size of 256 KB in the Standard tier and 1 MB in the Premium tier.. For more information about quotas, see Service Bus quotas.
Receive messages from a queue
Messages are received from a queue using the
receive_queue_message method on the ServiceBusService object:
msg = bus_service.receive_queue_message('taskqueue', peek_lock=False) print(msg.body)
Messages are deleted from the queue as they are read when the parameter
peek_lock is set to False. You can read (peek) and lock the message without deleting it from the queue by setting the parameter
peek_lock to True.
The behavior of reading and deleting the message as part of the receive operation.
If the
peek_lock parameter is set to True, the the delete method on the Message object. The delete method will mark the message as being consumed and remove it from the queue.
msg = bus_service.receive_queue_message('taskqueue', peek_lock=True) print(msg.body) msg.delete()
How to handle application crashes and unreadable messages
Service Bus provides functionality to help you gracefully recover from errors in your application or difficulties processing a message. If a receiver application is unable to process the message for some reason, then it can call the unlock method on the Message object. delete method is called, MessageId property of the message, which will remain constant across delivery attempts.
Next steps
Now that you have learned the basics of Service Bus queues, see these articles to learn more.