Skip to content

Commit ebc8313

Browse files
committed
[Python client] Lazily initialize thread pool in API client
The Python API client thread pool is only used with async calls and consumes system resources (threads). Many instances of the API client can reach system limits even in cases where there will never be any async call. This change initializes the API client thread pool on its first use.
1 parent 9409f83 commit ebc8313

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

modules/swagger-codegen/src/main/resources/python/api_client.mustache

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ApiClient(object):
6060
configuration = Configuration()
6161
self.configuration = configuration
6262

63-
self.pool = ThreadPool()
63+
self._pool = None # Use the pool property to lazily initialize the ThreadPool.
6464
self.rest_client = rest.RESTClientObject(configuration)
6565
self.default_headers = {}
6666
if header_name is not None:
@@ -70,8 +70,15 @@ class ApiClient(object):
7070
self.user_agent = '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/python{{/httpUserAgent}}'
7171

7272
def __del__(self):
73-
self.pool.close()
74-
self.pool.join()
73+
if self._pool is not None:
74+
self._pool.close()
75+
self._pool.join()
76+
77+
@property
78+
def pool(self):
79+
if self._pool is None:
80+
self._pool = ThreadPool()
81+
return self._pool
7582

7683
@property
7784
def user_agent(self):

samples/client/petstore/python-asyncio/petstore_api/api_client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
6666
configuration = Configuration()
6767
self.configuration = configuration
6868

69-
self.pool = ThreadPool()
69+
self._pool = None # Use the pool property to lazily initialize the ThreadPool.
7070
self.rest_client = rest.RESTClientObject(configuration)
7171
self.default_headers = {}
7272
if header_name is not None:
@@ -76,8 +76,15 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
7676
self.user_agent = 'Swagger-Codegen/1.0.0/python'
7777

7878
def __del__(self):
79-
self.pool.close()
80-
self.pool.join()
79+
if self._pool is not None:
80+
self._pool.close()
81+
self._pool.join()
82+
83+
@property
84+
def pool(self):
85+
if self._pool is None:
86+
self._pool = ThreadPool()
87+
return self._pool
8188

8289
@property
8390
def user_agent(self):

samples/client/petstore/python-tornado/petstore_api/api_client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
6767
configuration = Configuration()
6868
self.configuration = configuration
6969

70-
self.pool = ThreadPool()
70+
self._pool = None # Use the pool property to lazily initialize the ThreadPool.
7171
self.rest_client = rest.RESTClientObject(configuration)
7272
self.default_headers = {}
7373
if header_name is not None:
@@ -77,8 +77,15 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
7777
self.user_agent = 'Swagger-Codegen/1.0.0/python'
7878

7979
def __del__(self):
80-
self.pool.close()
81-
self.pool.join()
80+
if self._pool is not None:
81+
self._pool.close()
82+
self._pool.join()
83+
84+
@property
85+
def pool(self):
86+
if self._pool is None:
87+
self._pool = ThreadPool()
88+
return self._pool
8289

8390
@property
8491
def user_agent(self):

samples/client/petstore/python/petstore_api/api_client.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
6666
configuration = Configuration()
6767
self.configuration = configuration
6868

69-
self.pool = ThreadPool()
69+
self._pool = None # Use the pool property to lazily initialize the ThreadPool.
7070
self.rest_client = rest.RESTClientObject(configuration)
7171
self.default_headers = {}
7272
if header_name is not None:
@@ -76,8 +76,15 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
7676
self.user_agent = 'Swagger-Codegen/1.0.0/python'
7777

7878
def __del__(self):
79-
self.pool.close()
80-
self.pool.join()
79+
if self._pool is not None:
80+
self._pool.close()
81+
self._pool.join()
82+
83+
@property
84+
def pool(self):
85+
if self._pool is None:
86+
self._pool = ThreadPool()
87+
return self._pool
8188

8289
@property
8390
def user_agent(self):

0 commit comments

Comments
 (0)