Skip to content

Commit f23bd78

Browse files
Merge pull request #261598 from pauljewellmsft/py-async
Add async samples for Python lease articles
2 parents c03200b + 44bd34f commit f23bd78

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

articles/storage/blobs/storage-blob-container-lease-python.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: pauljewell
88

99
ms.service: azure-blob-storage
1010
ms.topic: how-to
11-
ms.date: 08/02/2023
11+
ms.date: 12/19/2023
1212
ms.devlang: python
1313
ms.custom: devx-track-python, devguide-python
1414
---
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
This article shows how to create and manage container leases using the [Azure Storage client library for Python](/python/api/overview/azure/storage). You can use the client library to acquire, renew, release and break container leases.
2121

22+
To learn about leasing a blob container using asynchronous APIs, see [Lease containers asynchronously](#lease-containers-asynchronously).
23+
2224
## Prerequisites
2325

2426
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Python. To learn about setting up your project, including package installation, adding `import` statements, and creating an authorized client object, see [Get started with Azure Blob Storage and Python](storage-blob-python-get-started.md).
@@ -83,6 +85,31 @@ The following example breaks the lease on a container:
8385

8486
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_break_container_lease":::
8587

88+
## Lease containers asynchronously
89+
90+
The Azure Blob Storage client library for Python supports leasing containers asynchronously. To learn more about project setup requirements, see [Asynchronous programming](storage-blob-python-get-started.md#asynchronous-programming).
91+
92+
Follow these steps to lease a container using asynchronous APIs:
93+
94+
1. Add the following import statements:
95+
96+
```python
97+
import asyncio
98+
99+
from azure.identity.aio import DefaultAzureCredential
100+
from azure.storage.blob.aio import BlobServiceClient, BlobLeaseClient
101+
```
102+
103+
1. Add code to run the program using `asyncio.run`. This function runs the passed coroutine, `main()` in our example, and manages the `asyncio` event loop. Coroutines are declared with the async/await syntax. In this example, the `main()` coroutine first creates the top level `BlobServiceClient` using `async with`, then calls the method that acquires the container lease. Note that only the top level client needs to use `async with`, as other clients created from it share the same connection pool.
104+
105+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_lease_container_async.py" id="Snippet_create_client_async":::
106+
107+
1. Add code to acquire a container lease. The code is the same as the synchronous example, except that the method is declared with the `async` keyword and the `await` keyword is used when calling the `acquire` method.
108+
109+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_lease_container_async.py" id="Snippet_acquire_container_lease":::
110+
111+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
112+
86113
[!INCLUDE [storage-dev-guide-container-lease](../../../includes/storage-dev-guides/storage-dev-guide-container-lease.md)]
87114

88115
## Resources
@@ -97,7 +124,7 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
97124

98125
### Code samples
99126

100-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py)
127+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_lease_container.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_lease_container_async.py) code samples from this article (GitHub)
101128

102129
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
103130

articles/storage/blobs/storage-blob-lease-python.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: pauljewell
88

99
ms.service: azure-blob-storage
1010
ms.topic: how-to
11-
ms.date: 08/02/2023
11+
ms.date: 12/19/2023
1212
ms.devlang: python
1313
ms.custom: devx-track-python, devguide-python
1414
---
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
This article shows how to create and manage blob leases using the [Azure Storage client library for Python](/python/api/overview/azure/storage). You can use the client library to acquire, renew, release, and break blob leases.
2121

22+
To learn about leasing a blob using asynchronous APIs, see [Lease blobs asynchronously](#lease-blobs-asynchronously).
23+
2224
## Prerequisites
2325

2426
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Python. To learn about setting up your project, including package installation, adding `import` statements, and creating an authorized client object, see [Get started with Azure Blob Storage and Python](storage-blob-python-get-started.md).
@@ -83,6 +85,31 @@ The following example breaks the lease on a blob:
8385

8486
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_blobs.py" id="Snippet_break_blob_lease":::
8587

88+
## Lease blobs asynchronously
89+
90+
The Azure Blob Storage client library for Python supports leasing blobs asynchronously. To learn more about project setup requirements, see [Asynchronous programming](storage-blob-python-get-started.md#asynchronous-programming).
91+
92+
Follow these steps to lease a blob using asynchronous APIs:
93+
94+
1. Add the following import statements:
95+
96+
```python
97+
import asyncio
98+
99+
from azure.identity.aio import DefaultAzureCredential
100+
from azure.storage.blob.aio import BlobServiceClient, BlobLeaseClient
101+
```
102+
103+
1. Add code to run the program using `asyncio.run`. This function runs the passed coroutine, `main()` in our example, and manages the `asyncio` event loop. Coroutines are declared with the async/await syntax. In this example, the `main()` coroutine first creates the top level `BlobServiceClient` using `async with`, then calls the method that acquires the blob lease. Note that only the top level client needs to use `async with`, as other clients created from it share the same connection pool.
104+
105+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_lease_blobs_async.py" id="Snippet_create_client_async":::
106+
107+
1. Add code to acquire a blob lease. The code is the same as the synchronous example, except that the method is declared with the `async` keyword and the `await` keyword is used when calling the `acquire_lease` method.
108+
109+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_lease_blobs_async.py" id="Snippet_acquire_blob_lease":::
110+
111+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
112+
86113
[!INCLUDE [storage-dev-guide-blob-lease](../../../includes/storage-dev-guides/storage-dev-guide-blob-lease.md)]
87114

88115
## Resources
@@ -97,7 +124,7 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
97124

98125
### Code samples
99126

100-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_blobs.py)
127+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_lease_blobs.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_lease_blobs_async.py) code samples from this article (GitHub)
101128

102129
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
103130

0 commit comments

Comments
 (0)