{"id":2585,"date":"2023-04-05T08:36:14","date_gmt":"2023-04-05T15:36:14","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sdk\/?p=2585"},"modified":"2023-04-05T09:27:42","modified_gmt":"2023-04-05T16:27:42","slug":"transport-sharing-in-azure-sdk-for-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sdk\/transport-sharing-in-azure-sdk-for-python\/","title":{"rendered":"Transport sharing in the Azure SDK for Python"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>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.<\/p>\n<h2>Understanding Transport Sharing<\/h2>\n<p>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.<\/p>\n<h2>Benefits of Sharing a Transport<\/h2>\n<p>Sharing a transport in the Azure SDK for Python offers several advantages:<\/p>\n<ul>\n<li>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.<\/li>\n<li>Efficiency: Sharing a transport can help conserve resources on both the client and the Azure service, which can reduce costs and improve scalability.<\/li>\n<li>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.<\/li>\n<\/ul>\n<h2>Implementing Transport Sharing<\/h2>\n<p>Here is an example demonstrating how to share a RequestsTransport transport in the Azure SDK for Python:<\/p>\n<pre><code class=\"language-python\">shared_transport = RequestsTransport()\r\nwith shared_transport:\r\n    blob_service_client1 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )\r\n    blob_service_client2 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )<\/code><\/pre>\n<p>In this example, both <code>blob_service_client1<\/code> and <code>blob_service_client2<\/code> use the same <code>RequestsTransport<\/code> transport. The underlying network connection is reused for both clients, which can improve performance by reducing the overhead of creating and tearing down connections.<\/p>\n<p>To share a transport object, set <code>session_owner<\/code> to <code>False<\/code> to inform the clients that the transport is externally managed and that the session should not be closed when the client is closed.<\/p>\n<p>For the async version, follow this example:<\/p>\n<pre><code class=\"language-python\">shared_transport = AioHttpTransport()\r\nasync with shared_transport:\r\n    blob_service_client1 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )\r\n    blob_service_client2 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )<\/code><\/pre>\n<p>To configure connection pooling, create a customized session object and use it when creating the transport. Here&#8217;s an example:<\/p>\n<pre><code class=\"language-python\">session = requests.Session()\r\nadapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100)\r\nsession.mount(\"http:\/\/\", adapter)\r\nsession.mount(\"https:\/\/\", adapter)\r\nshared_transport = RequestsTransport(session=session)\r\nwith shared_transport:\r\n    blob_service_client1 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )\r\n    blob_service_client2 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )<\/code><\/pre>\n<p>For the async version, use this example:<\/p>\n<pre><code class=\"language-python\">conn = aiohttp.TCPConnector(limit=100)\r\nsession = aiohttp.ClientSession(connector=conn)\r\nshared_transport = AioHttpTransport(session=session)\r\nasync with shared_transport:\r\n    blob_service_client1 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )\r\n    blob_service_client2 = BlobServiceClient.from_connection_string(\r\n        connection_string, transport=shared_transport, session_owner=False\r\n    )<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to improve the performance and efficiency of your Azure SDK for Python applications by sharing a transport object across multiple clients or requests.<\/p>\n","protected":false},"author":41895,"featured_media":2593,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[734,750,706,685,900,162],"class_list":["post-2585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sdk","tag-azure","tag-azure-sdk","tag-azuresdk","tag-http-transport","tag-performance","tag-python"],"acf":[],"blog_post_summary":"<p>Learn how to improve the performance and efficiency of your Azure SDK for Python applications by sharing a transport object across multiple clients or requests.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/users\/41895"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/comments?post=2585"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/posts\/2585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media\/2593"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/media?parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/categories?post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sdk\/wp-json\/wp\/v2\/tags?post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}