Skip to content

Commit 87ed9e2

Browse files
annatischRakshith Bhyravabhotla
authored and
Rakshith Bhyravabhotla
committed
[storage] Queues samples updates (#6732)
* Updated install instructions * wrong flag * Updated queues async samples * Updated readme * Re-recorded test * Updated recording * minor changes (#18)
1 parent 8ccb723 commit 87ed9e2

14 files changed

+860
-195
lines changed

sdk/storage/azure-storage-queue/README.md

+43-4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ from azure.storage.queue import QueueClient
114114
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
115115
queue.create_queue()
116116
```
117+
Create a queue asynchronously.
118+
```python
119+
from azure.storage.queue.aio import QueueClient
120+
121+
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
122+
await queue.create_queue()
123+
```
117124
### Enqueue messages
118125
Enqueue a message in your queue.
119126

@@ -124,6 +131,15 @@ queue = QueueClient.from_connection_string(conn_str="my_connection_string", queu
124131
queue.enqueue_message("I'm using queues!")
125132
queue.enqueue_message("This is my second message")
126133
```
134+
Enqueue messages with an async client
135+
```python
136+
from azure.storage.queue.aio import QueueClient
137+
138+
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
139+
await asyncio.gather(
140+
queue.enqueue_message("I'm using queues!"),
141+
queue.enqueue_message("This is my second message"))
142+
```
127143

128144
### Receive messages
129145
Receive messages from your queue.
@@ -136,11 +152,34 @@ response = queue.receive_messages()
136152

137153
for message in response:
138154
print(message.content)
155+
queue.delete_message(message)
139156

140157
# Printed messages from the front of the queue
141158
# >>I'm using queues!
142159
# >>This is my second message
143160
```
161+
Receive messages by batch.
162+
```python
163+
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
164+
response = queue.receive_messages(messages_per_page=10)
165+
166+
for message_batch in response.by_page():
167+
for message in message_batch:
168+
print(message.content)
169+
queue.delete_message(message)
170+
```
171+
Receive messages asynchronously:
172+
```python
173+
from azure.storage.queue.aio import QueueClient
174+
175+
queue = QueueClient.from_connection_string(conn_str="my_connection_string", queue="myqueue")
176+
response = queue.receive_messages()
177+
178+
async for message in response:
179+
print(message.content)
180+
await queue.delete_message(message)
181+
182+
```
144183

145184
## Troubleshooting
146185
Storage Queue clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/docs/exceptions.md).
@@ -154,25 +193,25 @@ Get started with our [Queue samples](https://github.com/Azure/azure-sdk-for-pyth
154193

155194
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:
156195

157-
* [`test_queue_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world.py) - Examples found in this article:
196+
* [`test_queue_samples_hello_world.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_hello_world_async.py)) - Examples found in this article:
158197
* Client creation
159198
* Create a queue
160199
* Enqueue messages
161200
* Receive messages
162201

163-
* [`test_queue_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication.py) - Examples for authenticating and creating the client:
202+
* [`test_queue_samples_authentication.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_authentication_async.py)) - Examples for authenticating and creating the client:
164203
* From a connection string
165204
* From a shared access key
166205
* From a shared access signature token
167206
* From active directory
168207

169-
* [`test_queue_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service.py) - Examples for interacting with the queue service:
208+
* [`test_queue_samples_service.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_service_async.py)) - Examples for interacting with the queue service:
170209
* Get and set service properties
171210
* List queues in a storage account
172211
* Create and delete a queue from the service
173212
* Get the QueueClient
174213

175-
* [`test_queue_samples_message.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message.py) - Examples for working with queues and messages:
214+
* [`test_queue_samples_message.py`](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message.py) ([async version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-queue/tests/test_queue_samples_message_async.py)) - Examples for working with queues and messages:
176215
* Set an access policy
177216
* Get and set queue metadata
178217
* Enqueue and receive messages

sdk/storage/azure-storage-queue/azure/storage/queue/aio/queue_client_async.py

+43-36
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@ class QueueClient(AsyncStorageAccountHostsMixin, QueueClientBase):
8585
shared access key, or an instance of a TokenCredentials class from azure.identity.
8686
8787
Example:
88-
.. literalinclude:: ../tests/test_queue_samples_message.py
89-
:start-after: [START create_queue_client]
90-
:end-before: [END create_queue_client]
88+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
89+
:start-after: [START async_create_queue_client]
90+
:end-before: [END async_create_queue_client]
9191
:language: python
9292
:dedent: 12
9393
:caption: Create the queue client with url and credential.
94+
95+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
96+
:start-after: [START async_create_queue_client_from_connection_string]
97+
:end-before: [END async_create_queue_client_from_connection_string]
98+
:language: python
99+
:dedent: 8
100+
:caption: Create the queue client with a connection string.
94101
"""
95102

96103
def __init__(
@@ -127,9 +134,9 @@ async def create_queue(self, metadata=None, timeout=None, **kwargs): # type: ig
127134
~azure.storage.queue._generated.models._models.StorageErrorException
128135
129136
Example:
130-
.. literalinclude:: ../tests/test_queue_samples_hello_world.py
131-
:start-after: [START create_queue]
132-
:end-before: [END create_queue]
137+
.. literalinclude:: ../tests/test_queue_samples_hello_world_async.py
138+
:start-after: [START async_create_queue]
139+
:end-before: [END async_create_queue]
133140
:language: python
134141
:dedent: 8
135142
:caption: Create a queue.
@@ -161,9 +168,9 @@ async def delete_queue(self, timeout=None, **kwargs): # type: ignore
161168
:rtype: None
162169
163170
Example:
164-
.. literalinclude:: ../tests/test_queue_samples_hello_world.py
165-
:start-after: [START delete_queue]
166-
:end-before: [END delete_queue]
171+
.. literalinclude:: ../tests/test_queue_samples_hello_world_async.py
172+
:start-after: [START async_delete_queue]
173+
:end-before: [END async_delete_queue]
167174
:language: python
168175
:dedent: 12
169176
:caption: Delete a queue.
@@ -186,9 +193,9 @@ async def get_queue_properties(self, timeout=None, **kwargs): # type: ignore
186193
:rtype: ~azure.storage.queue.models.QueueProperties
187194
188195
Example:
189-
.. literalinclude:: ../tests/test_queue_samples_message.py
190-
:start-after: [START get_queue_properties]
191-
:end-before: [END get_queue_properties]
196+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
197+
:start-after: [START async_get_queue_properties]
198+
:end-before: [END async_get_queue_properties]
192199
:language: python
193200
:dedent: 12
194201
:caption: Get the properties on the queue.
@@ -217,9 +224,9 @@ async def set_queue_metadata(self, metadata=None, timeout=None, **kwargs): # ty
217224
The server timeout, expressed in seconds.
218225
219226
Example:
220-
.. literalinclude:: ../tests/test_queue_samples_message.py
221-
:start-after: [START set_queue_metadata]
222-
:end-before: [END set_queue_metadata]
227+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
228+
:start-after: [START async_set_queue_metadata]
229+
:end-before: [END async_set_queue_metadata]
223230
:language: python
224231
:dedent: 12
225232
:caption: Set metadata on the queue.
@@ -278,9 +285,9 @@ async def set_queue_access_policy(self, signed_identifiers=None, timeout=None, *
278285
The server timeout, expressed in seconds.
279286
280287
Example:
281-
.. literalinclude:: ../tests/test_queue_samples_message.py
282-
:start-after: [START set_access_policy]
283-
:end-before: [END set_access_policy]
288+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
289+
:start-after: [START async_set_access_policy]
290+
:end-before: [END async_set_access_policy]
284291
:language: python
285292
:dedent: 12
286293
:caption: Set an access policy on the queue.
@@ -350,9 +357,9 @@ async def enqueue_message( # type: ignore
350357
:rtype: ~azure.storage.queue.models.QueueMessage
351358
352359
Example:
353-
.. literalinclude:: ../tests/test_queue_samples_message.py
354-
:start-after: [START enqueue_messages]
355-
:end-before: [END enqueue_messages]
360+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
361+
:start-after: [START async_enqueue_messages]
362+
:end-before: [END async_enqueue_messages]
356363
:language: python
357364
:dedent: 12
358365
:caption: Enqueue messages.
@@ -414,9 +421,9 @@ def receive_messages(self, messages_per_page=None, visibility_timeout=None, time
414421
:rtype: ~azure.core.async_paging.AsyncItemPaged[~azure.storage.queue.models.Message]
415422
416423
Example:
417-
.. literalinclude:: ../tests/test_queue_samples_message.py
418-
:start-after: [START receive_messages]
419-
:end-before: [END receive_messages]
424+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
425+
:start-after: [START async_receive_messages]
426+
:end-before: [END async_receive_messages]
420427
:language: python
421428
:dedent: 12
422429
:caption: Receive messages from the queue.
@@ -484,9 +491,9 @@ async def update_message(
484491
:rtype: ~azure.storage.queue.models.QueueMessage
485492
486493
Example:
487-
.. literalinclude:: ../tests/test_queue_samples_message.py
488-
:start-after: [START update_message]
489-
:end-before: [END update_message]
494+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
495+
:start-after: [START async_update_message]
496+
:end-before: [END async_update_message]
490497
:language: python
491498
:dedent: 12
492499
:caption: Update a message.
@@ -567,9 +574,9 @@ async def peek_messages(self, max_messages=None, timeout=None, **kwargs): # typ
567574
:rtype: list(:class:`~azure.storage.queue.models.QueueMessage`)
568575
569576
Example:
570-
.. literalinclude:: ../tests/test_queue_samples_message.py
571-
:start-after: [START peek_message]
572-
:end-before: [END peek_message]
577+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
578+
:start-after: [START async_peek_message]
579+
:end-before: [END async_peek_message]
573580
:language: python
574581
:dedent: 12
575582
:caption: Peek messages.
@@ -599,9 +606,9 @@ async def clear_messages(self, timeout=None, **kwargs): # type: ignore
599606
The server timeout, expressed in seconds.
600607
601608
Example:
602-
.. literalinclude:: ../tests/test_queue_samples_message.py
603-
:start-after: [START clear_messages]
604-
:end-before: [END clear_messages]
609+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
610+
:start-after: [START async_clear_messages]
611+
:end-before: [END async_clear_messages]
605612
:language: python
606613
:dedent: 12
607614
:caption: Clears all messages.
@@ -635,9 +642,9 @@ async def delete_message(self, message, pop_receipt=None, timeout=None, **kwargs
635642
The server timeout, expressed in seconds.
636643
637644
Example:
638-
.. literalinclude:: ../tests/test_queue_samples_message.py
639-
:start-after: [START delete_message]
640-
:end-before: [END delete_message]
645+
.. literalinclude:: ../tests/test_queue_samples_message_async.py
646+
:start-after: [START async_delete_message]
647+
:end-before: [END async_delete_message]
641648
:language: python
642649
:dedent: 12
643650
:caption: Delete a message.

sdk/storage/azure-storage-queue/azure/storage/queue/aio/queue_service_client_async.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,19 @@ class QueueServiceClient(AsyncStorageAccountHostsMixin, QueueServiceClientBase):
7575
shared access key, or an instance of a TokenCredentials class from azure.identity.
7676
7777
Example:
78-
.. literalinclude:: ../tests/test_queue_samples_authentication.py
79-
:start-after: [START create_queue_service_client]
80-
:end-before: [END create_queue_service_client]
78+
.. literalinclude:: ../tests/test_queue_samples_authentication_async.py
79+
:start-after: [START async_create_queue_service_client]
80+
:end-before: [END async_create_queue_service_client]
8181
:language: python
8282
:dedent: 8
8383
:caption: Creating the QueueServiceClient with an account url and credential.
84+
85+
.. literalinclude:: ../tests/test_queue_samples_authentication_async.py
86+
:start-after: [START async_create_queue_service_client_token]
87+
:end-before: [END async_create_queue_service_client_token]
88+
:language: python
89+
:dedent: 8
90+
:caption: Creating the QueueServiceClient with Azure Identity credentials.
8491
"""
8592

8693
def __init__(
@@ -142,9 +149,9 @@ async def get_service_properties(self, timeout=None, **kwargs): # type: ignore
142149
:rtype: ~azure.storage.queue._generated.models._models.StorageServiceProperties
143150
144151
Example:
145-
.. literalinclude:: ../tests/test_queue_samples_service.py
146-
:start-after: [START get_queue_service_properties]
147-
:end-before: [END get_queue_service_properties]
152+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
153+
:start-after: [START async_get_queue_service_properties]
154+
:end-before: [END async_get_queue_service_properties]
148155
:language: python
149156
:dedent: 8
150157
:caption: Getting queue service properties.
@@ -191,9 +198,9 @@ async def set_service_properties( # type: ignore
191198
:rtype: None
192199
193200
Example:
194-
.. literalinclude:: ../tests/test_queue_samples_service.py
195-
:start-after: [START set_queue_service_properties]
196-
:end-before: [END set_queue_service_properties]
201+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
202+
:start-after: [START async_set_queue_service_properties]
203+
:end-before: [END async_set_queue_service_properties]
197204
:language: python
198205
:dedent: 8
199206
:caption: Setting queue service properties.
@@ -238,9 +245,9 @@ def list_queues(
238245
:rtype: ~azure.core.paging.AsyncItemPaged[~azure.core.queue.models.QueueProperties]
239246
240247
Example:
241-
.. literalinclude:: ../tests/test_queue_samples_service.py
242-
:start-after: [START qsc_list_queues]
243-
:end-before: [END qsc_list_queues]
248+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
249+
:start-after: [START async_qsc_list_queues]
250+
:end-before: [END async_qsc_list_queues]
244251
:language: python
245252
:dedent: 12
246253
:caption: List queues in the service.
@@ -277,12 +284,12 @@ async def create_queue( # type: ignore
277284
:type metadata: dict(str, str)
278285
:param int timeout:
279286
The timeout parameter is expressed in seconds.
280-
:rtype: ~azure.storage.queue.queue_client.QueueClient
287+
:rtype: ~azure.storage.queue.aio.queue_client_async.QueueClient
281288
282289
Example:
283-
.. literalinclude:: ../tests/test_queue_samples_service.py
284-
:start-after: [START qsc_create_queue]
285-
:end-before: [END qsc_create_queue]
290+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
291+
:start-after: [START async_qsc_create_queue]
292+
:end-before: [END async_qsc_create_queue]
286293
:language: python
287294
:dedent: 8
288295
:caption: Create a queue in the service.
@@ -318,9 +325,9 @@ async def delete_queue( # type: ignore
318325
:rtype: None
319326
320327
Example:
321-
.. literalinclude:: ../tests/test_queue_samples_service.py
322-
:start-after: [START qsc_delete_queue]
323-
:end-before: [END qsc_delete_queue]
328+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
329+
:start-after: [START async_qsc_delete_queue]
330+
:end-before: [END async_qsc_delete_queue]
324331
:language: python
325332
:dedent: 12
326333
:caption: Delete a queue in the service.
@@ -339,12 +346,12 @@ def get_queue_client(self, queue, **kwargs):
339346
or an instance of QueueProperties.
340347
:type queue: str or ~azure.storage.queue.models.QueueProperties
341348
:returns: A :class:`~azure.core.queue.queue_client.QueueClient` object.
342-
:rtype: ~azure.core.queue.queue_client.QueueClient
349+
:rtype: ~azure.storage.queue.aio.queue_client_async.QueueClient
343350
344351
Example:
345-
.. literalinclude:: ../tests/test_queue_samples_service.py
346-
:start-after: [START get_queue_client]
347-
:end-before: [END get_queue_client]
352+
.. literalinclude:: ../tests/test_queue_samples_service_async.py
353+
:start-after: [START async_get_queue_client]
354+
:end-before: [END async_get_queue_client]
348355
:language: python
349356
:dedent: 8
350357
:caption: Get the queue client.

sdk/storage/azure-storage-queue/azure/storage/queue/queue_service_client.py

+7
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ class QueueServiceClient(StorageAccountHostsMixin):
8080
:language: python
8181
:dedent: 8
8282
:caption: Creating the QueueServiceClient with an account url and credential.
83+
84+
.. literalinclude:: ../tests/test_queue_samples_authentication.py
85+
:start-after: [START create_queue_service_client_token]
86+
:end-before: [END create_queue_service_client_token]
87+
:language: python
88+
:dedent: 8
89+
:caption: Creating the QueueServiceClient with Azure Identity credentials.
8390
"""
8491

8592
def __init__(

0 commit comments

Comments
 (0)