Azure Storage Queues is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account.
Common uses of Queue storage include:
- Creating a backlog of work to process asynchronously
- Passing messages between different parts of a distributed application
Source code | Package (PyPi) | API reference documentation | Product documentation | Samples
Install the Azure Storage Queue client library for Python with pip:
pip install azure-storage-queue --pre
Prerequisites: You must have an Azure subscription, and a Storage Account to use this package.
To create a Storage Account, you can use the Azure Portal, Azure PowerShell or Azure CLI:
az storage account create -n MyStorageAccountName -g MyResourceGroupName
Requires Python 2.7, 3.5 or later to use this package.
Interaction with Storage Queues starts with an instance of the QueueServiceClient class. You need an existing storage account, its URL, and a credential to instantiate the client object.
To authenticate the client you have a few options:
- Use a SAS token string
- Use an account shared access key
- Use a token credential from azure.identity
Alternatively, you can authenticate with a storage connection string using the from_connection_string
method. See example: Client creation with a connection string.
You can omit the credential if your account URL already has a SAS token.
Once you have your account URL and credentials ready, you can create the QueueServiceClient:
from azure.storage.queue import QueueServiceClient
service = QueueServiceClient(account_url="https://<my-storage-account-name>.queue.core.windows.net/", credential=credential)
The Queue service contains the following components:
- The storage account
- A queue which contains a set of messages
- A message, in any format, of up to 64 KB
The Storage Queues SDK provides two different clients to interact with the Queues Service:
- QueueServiceClient - this client interacts with the Queue Service at the account level.
It provides operations to retrieve and configure the account properties
as well as list, create, and delete queues within the account.
For operations relating to a specific queue, a client for that entity
can also be retrieved using the
get_queue_client
function. - QueueClient - this client represents interaction with a specific queue, although that queue need not exist yet. It provides operations to create, delete, or configure queues and includes operations to enqueue, receive, peak, delete, and update messages in the queue.
Once you've initialized a Client, you can use the following operations to work with the messages in the queue:
- Enqueue - Adds a message to the queue and optionally sets a visibility timeout for the message.
- Receive - Retrieves a message from the queue and makes it invisible to other consumers.
- Peek - Retrieves a message from the front of the queue, without changing the message visibility.
- Update - Updates the visibility timeout of a message and/or the message contents.
- Delete - Deletes a specified message from the queue.
- Clear - Clears all messages from the queue.
The following sections provide several code snippets covering some of the most common Storage Queue tasks, including:
Create the QueueServiceClient using the connection string to your Azure Storage account.
from azure.storage.queue import QueueServiceClient
service = QueueServiceClient.from_connection_string(conn_str="my_connection_string")
Create a queue in your storage account.
from azure.storage.queue import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
queue.create_queue()
Create a queue asynchronously.
from azure.storage.queue.aio import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
await queue.create_queue()
Enqueue a message in your queue.
from azure.storage.queue import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
queue.enqueue_message("I'm using queues!")
queue.enqueue_message("This is my second message")
Enqueue messages with an async client
from azure.storage.queue.aio import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
await asyncio.gather(
queue.enqueue_message("I'm using queues!"),
queue.enqueue_message("This is my second message"))
Receive messages from your queue.
from azure.storage.queue import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
response = queue.receive_messages()
for message in response:
print(message.content)
queue.delete_message(message)
# Printed messages from the front of the queue
# >>I'm using queues!
# >>This is my second message
Receive messages by batch.
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
response = queue.receive_messages(messages_per_page=10)
for message_batch in response.by_page():
for message in message_batch:
print(message.content)
queue.delete_message(message)
Receive messages asynchronously:
from azure.storage.queue.aio import QueueClient
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
response = queue.receive_messages()
async for message in response:
print(message.content)
await queue.delete_message(message)
Storage Queue clients raise exceptions defined in Azure Core.
All Queue service operations will throw a StorageErrorException on failure with helpful error codes.
Get started with our Queue samples.
Several Storage Queues Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Storage Queues:
-
test_queue_samples_hello_world.py
(async version) - Examples found in this article:- Client creation
- Create a queue
- Enqueue messages
- Receive messages
-
test_queue_samples_authentication.py
(async version) - Examples for authenticating and creating the client:- From a connection string
- From a shared access key
- From a shared access signature token
- From active directory
-
test_queue_samples_service.py
(async version) - Examples for interacting with the queue service:- Get and set service properties
- List queues in a storage account
- Create and delete a queue from the service
- Get the QueueClient
-
test_queue_samples_message.py
(async version) - Examples for working with queues and messages:- Set an access policy
- Get and set queue metadata
- Enqueue and receive messages
- Delete specified messages and clear all messages
- Peek and update messages
For more extensive documentation on the Azure Storage Queues, see the Azure Storage Queues documentation on docs.microsoft.com.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.