-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: avoid creating unused ThreadPools (#91)
* feat: avoid creating unused ThreadPools * docs: add requested TODOs
- Loading branch information
1 parent
9cf6542
commit 9d14077
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
61a62 | ||
> _pool = None | ||
64c65 | ||
< cookie=None): | ||
--- | ||
> cookie=None, pool_threads=None): | ||
67a69 | ||
> self.pool_threads = pool_threads | ||
69d70 | ||
< self.pool = ThreadPool() | ||
79,80c80,92 | ||
< 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters