feat: avoid creating unused ThreadPools (#91)

* feat: avoid creating unused ThreadPools

* docs: add requested TODOs
This commit is contained in:
Tomasz Prus
2018-12-17 05:01:12 +01:00
committed by Kubernetes Prow Robot
parent 9cf654298a
commit 9d14077ba1
4 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
--- a/kubernetes/client/api_client.py
+++ b/kubernetes/client/api_client.py
@@ -58,13 +58,15 @@ class ApiClient(object):
'datetime': datetime,
'object': object,
}
+ _pool = None
- def __init__(self, configuration=None, header_name=None, header_value=None, cookie=None):
+ def __init__(self, configuration=None, header_name=None, header_value=None,
+ cookie=None, pool_threads=None):
if configuration is None:
configuration = Configuration()
self.configuration = configuration
+ self.pool_threads = pool_threads
- self.pool = ThreadPool()
self.rest_client = RESTClientObject(configuration)
self.default_headers = {}
if header_name is not None:
@@ -74,8 +76,19 @@ class ApiClient(object):
self.user_agent = 'Swagger-Codegen/8.0.0-snapshot/python'
def __del__(self):
- self.pool.close()
- self.pool.join()
+ if self._pool:
+ self._pool.close()
+ self._pool.join()
+ self._pool = None
+
+ @property
+ def pool(self):
+ """Create thread pool on first request
+ avoids instantiating unused threadpool for blocking clients.
+ """
+ if self._pool is None:
+ self._pool = ThreadPool(self.pool_threads)
+ return self._pool
@property
def user_agent(self):