Skip to content

Commit 67bb824

Browse files
gavin-aguiarVictoria Hall
andauthored
fix: Fixed default threadpool count for pyver> 3.9 (#1575)
* Fixed default threadpool count for pyver> 3.9 * ptptc test fixes * test fixes + adding 312 test --------- Co-authored-by: Victoria Hall <[email protected]>
1 parent ceac558 commit 67bb824

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

azure_functions_worker/dispatcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .bindings.shared_memory_data_transfer import SharedMemoryManager
2525
from .constants import (
2626
APPLICATIONINSIGHTS_CONNECTION_STRING,
27+
HTTP_URI,
2728
METADATA_PROPERTIES_WORKER_INDEXED,
2829
PYTHON_AZURE_MONITOR_LOGGER_NAME,
2930
PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT,
@@ -39,8 +40,7 @@
3940
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT,
4041
PYTHON_THREADPOOL_THREAD_COUNT_MAX_37,
4142
PYTHON_THREADPOOL_THREAD_COUNT_MIN,
42-
REQUIRES_ROUTE_PARAMETERS,
43-
HTTP_URI
43+
REQUIRES_ROUTE_PARAMETERS
4444
)
4545
from .extension import ExtensionManager
4646
from .http_v2 import (
@@ -966,7 +966,7 @@ def tp_max_workers_validator(value: str) -> bool:
966966

967967
# Starting Python 3.9, worker won't be putting a limit on the
968968
# max_workers count in the created threadpool.
969-
default_value = None if sys.version_info.minor == 9 \
969+
default_value = None if sys.version_info.minor >= 9 \
970970
else f'{PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT}'
971971

972972
max_workers = get_app_setting(setting=PYTHON_THREADPOOL_THREAD_COUNT,

tests/unittests/test_dispatcher.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
from azure_functions_worker import protos
1616
from azure_functions_worker.constants import (
17+
HTTP_URI,
1718
METADATA_PROPERTIES_WORKER_INDEXED,
1819
PYTHON_ENABLE_DEBUG_LOGGING,
1920
PYTHON_ENABLE_INIT_INDEXING,
2021
PYTHON_THREADPOOL_THREAD_COUNT,
2122
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT,
2223
PYTHON_THREADPOOL_THREAD_COUNT_MAX_37,
23-
PYTHON_THREADPOOL_THREAD_COUNT_MIN, HTTP_URI, REQUIRES_ROUTE_PARAMETERS,
24+
PYTHON_THREADPOOL_THREAD_COUNT_MIN,
25+
REQUIRES_ROUTE_PARAMETERS
2426
)
2527
from azure_functions_worker.dispatcher import Dispatcher, ContextEnabledTask
2628
from azure_functions_worker.version import VERSION
@@ -550,15 +552,15 @@ async def test_dispatcher_sync_threadpool_in_placeholder_above_max(self):
550552
"as the default passed is None, the cpu_count determines the "
551553
"number of max_workers and we cannot mock the os.cpu_count() "
552554
"in the concurrent.futures.ThreadPoolExecutor")
553-
class TestThreadPoolSettingsPython39(TestThreadPoolSettingsPython38):
555+
class TestThreadPoolSettingsPython39(TestThreadPoolSettingsPython37):
554556
def setUp(self, version=SysVersionInfo(3, 9, 0, 'final', 0)):
555557
super(TestThreadPoolSettingsPython39, self).setUp(version)
556-
557558
self.mock_os_cpu = patch(
558559
'os.cpu_count', return_value=2)
559560
# 6 - based on 2 cores - min(32, (os.cpu_count() or 1) + 4) - 2 + 4
560561
self._default_workers: Optional[int] = 6
561562
self.mock_os_cpu.start()
563+
self._allowed_max_workers: int = self._over_max_workers
562564

563565
def tearDown(self):
564566
self.mock_os_cpu.stop()
@@ -570,11 +572,19 @@ def tearDown(self):
570572
"as the default passed is None, the cpu_count determines the "
571573
"number of max_workers and we cannot mock the os.cpu_count() "
572574
"in the concurrent.futures.ThreadPoolExecutor")
573-
class TestThreadPoolSettingsPython310(TestThreadPoolSettingsPython39):
575+
class TestThreadPoolSettingsPython310(TestThreadPoolSettingsPython37):
574576
def setUp(self, version=SysVersionInfo(3, 10, 0, 'final', 0)):
575577
super(TestThreadPoolSettingsPython310, self).setUp(version)
578+
self._allowed_max_workers: int = self._over_max_workers
579+
self.mock_os_cpu = patch(
580+
'os.cpu_count', return_value=2)
581+
# 6 - based on 2 cores - min(32, (os.cpu_count() or 1) + 4) - 2 + 4
582+
self._default_workers: Optional[int] = 6
583+
self.mock_os_cpu.start()
584+
self._allowed_max_workers: int = self._over_max_workers
576585

577586
def tearDown(self):
587+
self.mock_os_cpu.stop()
578588
super(TestThreadPoolSettingsPython310, self).tearDown()
579589

580590

@@ -583,12 +593,41 @@ def tearDown(self):
583593
"as the default passed is None, the cpu_count determines the "
584594
"number of max_workers and we cannot mock the os.cpu_count() "
585595
"in the concurrent.futures.ThreadPoolExecutor")
586-
class TestThreadPoolSettingsPython311(TestThreadPoolSettingsPython310):
596+
class TestThreadPoolSettingsPython311(TestThreadPoolSettingsPython37):
587597
def setUp(self, version=SysVersionInfo(3, 11, 0, 'final', 0)):
588598
super(TestThreadPoolSettingsPython311, self).setUp(version)
599+
self._allowed_max_workers: int = self._over_max_workers
600+
self.mock_os_cpu = patch(
601+
'os.cpu_count', return_value=2)
602+
# 6 - based on 2 cores - min(32, (os.cpu_count() or 1) + 4) - 2 + 4
603+
self._default_workers: Optional[int] = 6
604+
self.mock_os_cpu.start()
605+
self._allowed_max_workers: int = self._over_max_workers
589606

590607
def tearDown(self):
591-
super(TestThreadPoolSettingsPython310, self).tearDown()
608+
self.mock_os_cpu.stop()
609+
super(TestThreadPoolSettingsPython311, self).tearDown()
610+
611+
612+
@unittest.skipIf(sys.version_info.minor != 12,
613+
"Run the tests only for Python 3.12. In other platforms, "
614+
"as the default passed is None, the cpu_count determines the "
615+
"number of max_workers and we cannot mock the os.cpu_count() "
616+
"in the concurrent.futures.ThreadPoolExecutor")
617+
class TestThreadPoolSettingsPython312(TestThreadPoolSettingsPython37):
618+
def setUp(self, version=SysVersionInfo(3, 12, 0, 'final', 0)):
619+
super(TestThreadPoolSettingsPython312, self).setUp(version)
620+
self._allowed_max_workers: int = self._over_max_workers
621+
self.mock_os_cpu = patch(
622+
'os.cpu_count', return_value=2)
623+
# 6 - based on 2 cores - min(32, (os.cpu_count() or 1) + 4) - 2 + 4
624+
self._default_workers: Optional[int] = 6
625+
self.mock_os_cpu.start()
626+
self._allowed_max_workers: int = self._over_max_workers
627+
628+
def tearDown(self):
629+
self.mock_os_cpu.stop()
630+
super(TestThreadPoolSettingsPython312, self).tearDown()
592631

593632

594633
class TestDispatcherStein(testutils.AsyncTestCase):

0 commit comments

Comments
 (0)