[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:
Fabian von Feilitzsch
2020-01-28 21:58:11 -08:00
committed by GitHub
parent d627282e89
commit 15345e1620
58 changed files with 2850 additions and 2264 deletions

View File

@@ -11,6 +11,8 @@ $ nosetests -v
import os
import time
import atexit
import weakref
import unittest
from dateutil.parser import parse
@@ -169,3 +171,17 @@ class ApiClientTests(unittest.TestCase):
data = [pet]
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, list_of_pet_dict)
def test_context_manager_closes_threadpool(self):
with petstore_api.ApiClient() as client:
self.assertIsNotNone(client.pool)
pool_ref = weakref.ref(client._pool)
self.assertIsNotNone(pool_ref())
self.assertIsNone(pool_ref())
def test_atexit_closes_threadpool(self):
client = petstore_api.ApiClient()
self.assertIsNotNone(client.pool)
self.assertIsNotNone(client._pool)
atexit._run_exitfuncs()
self.assertIsNone(client._pool)