Skip to content

Commit b5ee484

Browse files
authored
Add sample for shared transport (Azure#28841)
* Add sample for shared transport * updates * updates * updates * update * black
1 parent 962f930 commit b5ee484

File tree

3 files changed

+162
-2
lines changed

3 files changed

+162
-2
lines changed

sdk/core/azure-core/samples/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ products:
66
- azure
77
---
88

9-
# Azure Core Library Python Samples
9+
# Azure Core Library Python Developer Samples
1010

1111
These are some code snippets that show the way in which you can set up some functionalities used in new SDKs such as logging, pipelines, etc.
1212

13-
They are not intended to be ran as standalone application, but show you how these functionalities can be configured.
13+
They are not intended to be run as standalone application, but show you how these functionalities can be configured.
1414

1515
[test_example_sync.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/test_example_sync.py) - samples of how to create a sync pipeline
1616

1717
[test_example_async.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/test_example_async.py) - samples of how to create an async pipeline
1818

1919
[test_example_sansio.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/test_example_sansio.py) - samples of how to config policies
20+
21+
# Azure Core Library Python End User Samples
22+
23+
These are some code snippets that show the way in which end users can customize the behavior.
24+
25+
[shared_transport.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/example_shared_transport.py) - samples of how to use a shared sync transport
26+
[shared_transport_async.py](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/samples/example_shared_transport_async.py) - samples of how to use a shared async transport
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.py
11+
12+
DESCRIPTION:
13+
This sample demonstrates how to share transport connections between multiple clients.
14+
15+
USAGE:
16+
python example_shared_transport.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+
from azure.core.pipeline.transport import RequestsTransport
24+
from azure.storage.blob import BlobServiceClient
25+
26+
connection_string = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
27+
28+
29+
def shared_transport():
30+
# [START shared_transport]
31+
shared_transport = RequestsTransport()
32+
with shared_transport:
33+
blob_service_client1 = BlobServiceClient.from_connection_string(
34+
connection_string,
35+
transport=shared_transport,
36+
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
37+
)
38+
blob_service_client2 = BlobServiceClient.from_connection_string(
39+
connection_string, transport=shared_transport, session_owner=False
40+
)
41+
containers1 = blob_service_client1.list_containers()
42+
for contain in containers1:
43+
print(contain.name)
44+
containers2 = blob_service_client2.list_containers()
45+
for contain in containers2:
46+
print(contain.name)
47+
# [END shared_transport]
48+
49+
50+
def shared_transport_with_pooling():
51+
# [START shared_transport_with_pooling]
52+
import requests
53+
54+
session = requests.Session()
55+
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
56+
session.mount("http://", adapter)
57+
session.mount("https://", adapter)
58+
shared_transport = RequestsTransport(session=session)
59+
with shared_transport:
60+
blob_service_client1 = BlobServiceClient.from_connection_string(
61+
connection_string, transport=shared_transport, session_owner=False
62+
)
63+
blob_service_client2 = BlobServiceClient.from_connection_string(
64+
connection_string, transport=shared_transport, session_owner=False
65+
)
66+
containers1 = blob_service_client1.list_containers()
67+
for contain in containers1:
68+
print(contain.name)
69+
containers2 = blob_service_client2.list_containers()
70+
for contain in containers2:
71+
print(contain.name)
72+
# [END shared_transport_with_pooling]
73+
74+
75+
if __name__ == "__main__":
76+
shared_transport()
77+
shared_transport_with_pooling()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)