|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for |
| 6 | +# license information. |
| 7 | +# -------------------------------------------------------------------------- |
| 8 | + |
| 9 | +""" |
| 10 | +FILE: example_shared_transport_async.py |
| 11 | +
|
| 12 | +DESCRIPTION: |
| 13 | + This sample demonstrates how to share transport connections between multiple async clients. |
| 14 | +
|
| 15 | +USAGE: |
| 16 | + python example_shared_transport_async.py |
| 17 | +
|
| 18 | + Set the environment variables with your own values before running the sample: |
| 19 | + 1) AZURE_STORAGE_CONNECTION_STRING - the endpoint of your Azure Metrics Advisor service |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import asyncio |
| 24 | +from azure.core.pipeline.transport import AioHttpTransport |
| 25 | +from azure.storage.blob.aio import BlobServiceClient |
| 26 | + |
| 27 | +connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING") |
| 28 | + |
| 29 | + |
| 30 | +async def shared_transport_async(): |
| 31 | + # [START shared_transport_async] |
| 32 | + shared_transport = AioHttpTransport() |
| 33 | + async with shared_transport: |
| 34 | + blob_service_client1 = BlobServiceClient.from_connection_string( |
| 35 | + connection_string, |
| 36 | + transport=shared_transport, |
| 37 | + session_owner=False, # here we set session_owner to False to indicate that we don't want to close the session when the client is closed |
| 38 | + ) |
| 39 | + blob_service_client2 = BlobServiceClient.from_connection_string( |
| 40 | + connection_string, transport=shared_transport, session_owner=False |
| 41 | + ) |
| 42 | + containers1 = blob_service_client1.list_containers() |
| 43 | + async for contain in containers1: |
| 44 | + print(contain.name) |
| 45 | + containers2 = blob_service_client2.list_containers() |
| 46 | + async for contain in containers2: |
| 47 | + print(contain.name) |
| 48 | + # [END shared_transport_async] |
| 49 | + |
| 50 | + |
| 51 | +async def shared_transport_async_with_pooling(): |
| 52 | + # [START shared_transport_async_with_pooling] |
| 53 | + import aiohttp |
| 54 | + |
| 55 | + conn = aiohttp.TCPConnector(limit=100) |
| 56 | + session = aiohttp.ClientSession(connector=conn) |
| 57 | + shared_transport = AioHttpTransport(session=session) |
| 58 | + async with shared_transport: |
| 59 | + blob_service_client1 = BlobServiceClient.from_connection_string( |
| 60 | + connection_string, transport=shared_transport, session_owner=False |
| 61 | + ) |
| 62 | + blob_service_client2 = BlobServiceClient.from_connection_string( |
| 63 | + connection_string, transport=shared_transport, session_owner=False |
| 64 | + ) |
| 65 | + containers1 = blob_service_client1.list_containers() |
| 66 | + async for contain in containers1: |
| 67 | + print(contain.name) |
| 68 | + containers2 = blob_service_client2.list_containers() |
| 69 | + async for contain in containers2: |
| 70 | + print(contain.name) |
| 71 | + # [END shared_transport_async_with_pooling] |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + asyncio.run(shared_transport_async()) |
| 76 | + asyncio.run(shared_transport_async_with_pooling()) |
0 commit comments