[python] Avoid creating unused ThreadPools (#1387)

* Avoid creating unused ThreadPools

Instead, create ApiClient.pool on first request for .pool property.

avoids spawning n-cpus threads (the default for ThreadPool) at instantiation of every ApiClient

* update doc

* set pool_thread to None
This commit is contained in:
William Cheng
2018-11-08 17:39:20 +08:00
committed by GitHub
parent 34945427d4
commit 2ef499faf3
16 changed files with 522 additions and 19 deletions

View File

@@ -1009,6 +1009,102 @@ class FakeApi(object):
_request_timeout=local_var_params.get('_request_timeout'),
collection_formats=collection_formats)
def test_group_parameters(self, **kwargs): # noqa: E501
"""Fake endpoint to test group parameters (optional) # noqa: E501
Fake endpoint to test group parameters (optional) # noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.test_group_parameters(async_req=True)
>>> result = thread.get()
:param async_req bool
:param int string_group: String in group parameters
:param bool boolean_group: Boolean in group parameters
:param int int64_group: Integer in group parameters
:return: None
If the method is called asynchronously,
returns the request thread.
"""
kwargs['_return_http_data_only'] = True
if kwargs.get('async_req'):
return self.test_group_parameters_with_http_info(**kwargs) # noqa: E501
else:
(data) = self.test_group_parameters_with_http_info(**kwargs) # noqa: E501
return data
def test_group_parameters_with_http_info(self, **kwargs): # noqa: E501
"""Fake endpoint to test group parameters (optional) # noqa: E501
Fake endpoint to test group parameters (optional) # noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.test_group_parameters_with_http_info(async_req=True)
>>> result = thread.get()
:param async_req bool
:param int string_group: String in group parameters
:param bool boolean_group: Boolean in group parameters
:param int int64_group: Integer in group parameters
:return: None
If the method is called asynchronously,
returns the request thread.
"""
local_var_params = locals()
all_params = ['string_group', 'boolean_group', 'int64_group'] # noqa: E501
all_params.append('async_req')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
all_params.append('_request_timeout')
for key, val in six.iteritems(local_var_params['kwargs']):
if key not in all_params:
raise TypeError(
"Got an unexpected keyword argument '%s'"
" to method test_group_parameters" % key
)
local_var_params[key] = val
del local_var_params['kwargs']
collection_formats = {}
path_params = {}
query_params = []
if 'string_group' in local_var_params:
query_params.append(('string_group', local_var_params['string_group'])) # noqa: E501
if 'int64_group' in local_var_params:
query_params.append(('int64_group', local_var_params['int64_group'])) # noqa: E501
header_params = {}
if 'boolean_group' in local_var_params:
header_params['boolean_group'] = local_var_params['boolean_group'] # noqa: E501
form_params = []
local_var_files = {}
body_params = None
# Authentication setting
auth_settings = [] # noqa: E501
return self.api_client.call_api(
'/fake', 'DELETE',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type=None, # noqa: E501
auth_settings=auth_settings,
async_req=local_var_params.get('async_req'),
_return_http_data_only=local_var_params.get('_return_http_data_only'), # noqa: E501
_preload_content=local_var_params.get('_preload_content', True),
_request_timeout=local_var_params.get('_request_timeout'),
collection_formats=collection_formats)
def test_inline_additional_properties(self, request_body, **kwargs): # noqa: E501
"""test inline additionalProperties # noqa: E501

View File

@@ -45,6 +45,8 @@ class ApiClient(object):
the API.
:param cookie: a cookie to include in the header when making calls
to the API
:param pool_threads: The number of threads to use for async requests
to the API. More threads means more concurrent API requests.
"""
PRIMITIVE_TYPES = (float, bool, bytes, six.text_type) + six.integer_types
@@ -58,14 +60,15 @@ class ApiClient(object):
'datetime': datetime.datetime,
'object': object,
}
_pool = None
def __init__(self, configuration=None, header_name=None, header_value=None,
cookie=None):
cookie=None, pool_threads=1):
if configuration is None:
configuration = Configuration()
self.configuration = configuration
self.pool_threads = pool_threads
self.pool = ThreadPool()
self.rest_client = rest.RESTClientObject(configuration)
self.default_headers = {}
if header_name is not None:
@@ -75,8 +78,19 @@ class ApiClient(object):
self.user_agent = 'OpenAPI-Generator/1.0.0/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):