forked from loafle/openapi-generator-original
[python] Cleanup ThreadPool with atexit rather than __del__ (#5094)
* [python] Cleanup ThreadPool with atexit rather than __del__ This removes the `__del__` function from the generated Python client, and replaces it with a `cleanup` function. When a ThreadPool is created, the cleanup function is registered with the `atexit` module. This fixes #5093, where the API client could hang indefinitely at garbage collection. * Update petstore examples * Test to ensure threadpool is cleaned up * Docs now encourage using the context manager * Regenerate docs * Update samples
This commit is contained in:
committed by
GitHub
parent
d627282e89
commit
15345e1620
@@ -10,6 +10,7 @@
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import atexit
|
||||
import datetime
|
||||
from dateutil.parser import parse
|
||||
import json
|
||||
@@ -80,11 +81,19 @@ class ApiClient(object):
|
||||
self.user_agent = 'OpenAPI-Generator/1.0.0/python'
|
||||
self.client_side_validation = configuration.client_side_validation
|
||||
|
||||
def __del__(self):
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
self.close()
|
||||
|
||||
def close(self):
|
||||
if self._pool:
|
||||
self._pool.close()
|
||||
self._pool.join()
|
||||
self._pool = None
|
||||
if hasattr(atexit, 'unregister'):
|
||||
atexit.unregister(self.close)
|
||||
|
||||
@property
|
||||
def pool(self):
|
||||
@@ -92,6 +101,7 @@ class ApiClient(object):
|
||||
avoids instantiating unused threadpool for blocking clients.
|
||||
"""
|
||||
if self._pool is None:
|
||||
atexit.register(self.close)
|
||||
self._pool = ThreadPool(self.pool_threads)
|
||||
return self._pool
|
||||
|
||||
|
||||
Reference in New Issue
Block a user