Transport sharing in the Azure SDK for Python

Xiang Yan

Introduction

In the Azure SDK for Python, sharing a transport object can help improve the performance and efficiency of your applications. This post explains what sharing a transport means, why you should do it, and how to implement it.

Understanding Transport Sharing

Sharing a transport refers to creating a single instance of a transport object, such as an HttpTransport, and using it across multiple clients or requests. When you create a new transport object for each client or request, it can lead to increased resource usage and reduced performance.

Benefits of Sharing a Transport

Sharing a transport in the Azure SDK for Python offers several advantages:

  • Performance: Sharing a transport can improve performance by reducing the number of connections that need to be created and maintained, which can be especially beneficial in high-concurrency scenarios.
  • Efficiency: Sharing a transport can help conserve resources on both the client and the Azure service, which can reduce costs and improve scalability.
  • Simplicity: Sharing a transport can make it easier to manage and maintain your application. Instead of having to manage multiple connections in different parts of your code, you can manage a single connection that is shared across your application.

Implementing Transport Sharing

Here is an example demonstrating how to share a RequestsTransport transport in the Azure SDK for Python:

shared_transport = RequestsTransport()
with shared_transport:
    blob_service_client1 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )
    blob_service_client2 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )

In this example, both blob_service_client1 and blob_service_client2 use the same RequestsTransport transport. The underlying network connection is reused for both clients, which can improve performance by reducing the overhead of creating and tearing down connections.

To share a transport object, set session_owner to False to inform the clients that the transport is externally managed and that the session should not be closed when the client is closed.

For the async version, follow this example:

shared_transport = AioHttpTransport()
async with shared_transport:
    blob_service_client1 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )
    blob_service_client2 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )

To configure connection pooling, create a customized session object and use it when creating the transport. Here’s an example:

session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)
session.mount("http://", adapter)
session.mount("https://", adapter)
shared_transport = RequestsTransport(session=session)
with shared_transport:
    blob_service_client1 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )
    blob_service_client2 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )

For the async version, use this example:

conn = aiohttp.TCPConnector(limit=100)
session = aiohttp.ClientSession(connector=conn)
shared_transport = AioHttpTransport(session=session)
async with shared_transport:
    blob_service_client1 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )
    blob_service_client2 = BlobServiceClient.from_connection_string(
        connection_string, transport=shared_transport, session_owner=False
    )

Conclusion

In summary, sharing a transport in Azure SDK can provide several benefits for your application, including improved performance, efficiency, and simplicity. With the examples provided, you can easily implement a shared transport in your Python application using Azure SDK.

Remember to choose the appropriate transport implementation for your use case and consider customizing the session object to achieve advanced configurations such as connection pooling.

0 comments

Discussion is closed.

Feedback usabilla icon