This guide will walk you through the essential steps of interacting with Azure Storage Queues using the official Azure SDK for Python. Azure Storage Queues provide a robust and scalable messaging solution for decoupling application components.
pip install azure-storage-queue
To connect to your Azure Storage Queue, you'll need your storage account's connection string. You can find this in the Azure portal under your storage account's "Access keys" section.
Import the necessary class and create an instance of the QueueClient, providing your connection string and queue name.
from azure.storage.queue import QueueClient
# Replace with your actual connection string and queue name
connection_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-python-queue"
# Create the QueueClient
try:
queue_client = QueueClient.from_connection_string(connection_str, queue_name)
print(f"Successfully connected to queue: {queue_name}")
except Exception as e:
print(f"Error connecting to queue: {e}")
You can send messages to the queue using the send_message method. Messages are typically strings, but can be any JSON-serializable object.
message_text = "Hello, Azure Queues!"
try:
response = queue_client.send_message(message_text)
print(f"Message sent: {message_text}")
print(f"Message ID: {response.get('message_id')}")
except Exception as e:
print(f"Error sending message: {e}")
You can also specify options like visibility_timeout (how long the message is hidden after being retrieved) and time_to_live (how long the message remains in the queue).
complex_message = {"task": "process_data", "payload": {"id": 123, "data": "sample"}}
try:
response = queue_client.send_message(
complex_message,
visibility_timeout=30, # Message hidden for 30 seconds
time_to_live=600 # Message expires after 10 minutes
)
print(f"Complex message sent. Message ID: {response.get('message_id')}")
except Exception as e:
print(f"Error sending complex message: {e}")
To process messages, you retrieve them from the queue. When you receive a message, it becomes invisible for a specified period (visibility_timeout) to prevent other consumers from processing it simultaneously.
try:
# Receive one message, making it invisible for 30 seconds
received_message = queue_client.receive_message(visibility_timeout=30)
if received_message:
print(f"Received message: {received_message.get('message_text')}")
print(f"Message ID: {received_message.get('message_id')}")
print(f"Pop Receipt: {received_message.get('pop_receipt')}")
# --- IMPORTANT ---
# After processing the message, you must delete it to remove it permanently.
# If you don't delete it, it will become visible again after the visibility_timeout.
# Example of deleting the message:
# queue_client.delete_message(received_message.get('message_id'), received_message.get('pop_receipt'))
# print("Message deleted successfully.")
else:
print("No messages in the queue.")
except Exception as e:
print(f"Error receiving message: {e}")
You can also receive multiple messages at once, up to a maximum of 32.
try:
# Receive up to 5 messages, making them invisible for 60 seconds
messages = queue_client.receive_messages(max_messages=5, visibility_timeout=60)
if messages:
print(f"Received {len(messages)} messages:")
for message in messages:
print(f" - Message ID: {message.get('message_id')}, Text: {message.get('message_text')}")
# Remember to delete messages after processing!
else:
print("No messages available to receive.")
except Exception as e:
print(f"Error receiving multiple messages: {e}")
Once a message has been successfully processed, it must be deleted from the queue. This requires the message's ID and its pop_receipt, which is returned when the message is received.
# Assuming 'received_message' is from a previous receive_message call
# and contains 'message_id' and 'pop_receipt'
# message_id_to_delete = received_message.get('message_id')
# pop_receipt_to_delete = received_message.get('pop_receipt')
# if message_id_to_delete and pop_receipt_to_delete:
# try:
# queue_client.delete_message(message_id_to_delete, pop_receipt_to_delete)
# print(f"Message {message_id_to_delete} deleted successfully.")
# except Exception as e:
# print(f"Error deleting message {message_id_to_delete}: {e}")
# else:
# print("Cannot delete message: Missing message ID or pop receipt.")
You can clear all messages from a queue or delete the queue entirely.
# try:
# queue_client.clear_messages()
# print(f"Queue '{queue_name}' cleared successfully.")
# except Exception as e:
# print(f"Error clearing queue '{queue_name}': {e}")
Note: Deleting a queue is a permanent operation.
# try:
# queue_client.delete_queue()
# print(f"Queue '{queue_name}' deleted successfully.")
# except Exception as e:
# print(f"Error deleting queue '{queue_name}': {e}")
json to serialize and deserialize messages.This guide provides a foundational understanding of using Azure Storage Queues with Python. For more detailed information and advanced scenarios, please refer to the official Azure Storage Queues documentation.