-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛addresses blocking Thread/Process pool executors when shutting down (#…
…2397) * refactor, no destruction is necessary * avoids destroying the pool and blocking * using non blocking process pool * replaces blocking processpool * replaces blocking processpoolexecutor * refactored * using try finally to ensure cleanup * refactored * pylint * fixing error * fixing pool creation * extracted pools module * marked as duplicate * refactored to use new shared code * transformed int a generator * fix error * it can be waited here * using system default threadpool * removing unused * removing unused * added testing dependency * cpython issue was casing a hang * added regression tests for pools * updated comments * fix pylint * cleanup comments * downgraded * fixed tests * update codestyle * updated non_blocking_process_pool_executor in catalog * adds tests and more comments * added fixme notes Co-authored-by: Andrei Neagu <[email protected]>
- Loading branch information
Showing
22 changed files
with
172 additions
and
54 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from concurrent.futures import ProcessPoolExecutor | ||
from contextlib import contextmanager | ||
|
||
# only gets created on use and is guaranteed to be the s | ||
# ame for the entire lifetime of the application | ||
__shared_process_pool_executor = {} | ||
|
||
|
||
def get_shared_process_pool_executor(**kwargs) -> ProcessPoolExecutor: | ||
# sometimes a pool requires a specific configuration | ||
# the key helps to distinguish between them in the same application | ||
key = "".join(sorted(["_".join((k, str(v))) for k, v in kwargs.items()])) | ||
|
||
if key not in __shared_process_pool_executor: | ||
# pylint: disable=consider-using-with | ||
__shared_process_pool_executor[key] = ProcessPoolExecutor(**kwargs) | ||
|
||
return __shared_process_pool_executor[key] | ||
|
||
|
||
@contextmanager | ||
def non_blocking_process_pool_executor(**kwargs) -> ProcessPoolExecutor: | ||
""" | ||
Avoids default context manger behavior which calls | ||
shutdown with wait=True an blocks. | ||
""" | ||
executor = get_shared_process_pool_executor(**kwargs) | ||
try: | ||
yield executor | ||
finally: | ||
# due to an issue in cpython https://bugs.python.org/issue34073 | ||
# bypassing shutdown and using a shared pool | ||
# remove call to get_shared_process_pool_executor and replace with | ||
# a new instance when the issue is fixed | ||
# FIXME: uncomment below line when the issue is fixed | ||
# executor.shutdown(wait=False) | ||
pass |
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
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
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 |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
import operator | ||
|
||
import attr | ||
|
||
from servicelib.incidents import BaseIncident, LimitedOrderedStack | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from asyncio import BaseEventLoop | ||
from concurrent.futures import ProcessPoolExecutor | ||
|
||
|
||
from servicelib.pools import non_blocking_process_pool_executor | ||
|
||
|
||
def return_int_one() -> int: | ||
return 1 | ||
|
||
|
||
async def test_default_thread_pool_executor(loop: BaseEventLoop) -> None: | ||
assert await loop.run_in_executor(None, return_int_one) == 1 | ||
|
||
|
||
async def test_blocking_process_pool_executor(loop: BaseEventLoop) -> None: | ||
assert await loop.run_in_executor(ProcessPoolExecutor(), return_int_one) == 1 | ||
|
||
|
||
async def test_non_blocking_process_pool_executor(loop: BaseEventLoop) -> None: | ||
with non_blocking_process_pool_executor() as executor: | ||
assert await loop.run_in_executor(executor, return_int_one) == 1 | ||
|
||
|
||
async def test_same_pool_instances() -> None: | ||
with non_blocking_process_pool_executor() as first, non_blocking_process_pool_executor() as second: | ||
assert first == second | ||
|
||
|
||
async def test_different_pool_instances() -> None: | ||
with non_blocking_process_pool_executor( | ||
max_workers=1 | ||
) as first, non_blocking_process_pool_executor() as second: | ||
assert first != second |
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
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
# pylint:disable=redefined-outer-name | ||
|
||
import pytest | ||
|
||
from servicelib import openapi | ||
|
||
|
||
|
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
|
||
import attr | ||
from aiohttp import web | ||
|
||
from servicelib.rest_codecs import DataEncoder | ||
|
||
|
||
|
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
Oops, something went wrong.