-
Notifications
You must be signed in to change notification settings - Fork 107
Add PYTHON_THREADPOOL_THREAD_COUNT app setting #744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
tests/unittests/dispatcher_functions/show_context/__init__.py
This file contains hidden or 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,13 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import json | ||
import azure.functions as func | ||
|
||
|
||
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse: | ||
result = { | ||
'function_directory': context.function_directory, | ||
'function_name': context.function_name | ||
} | ||
return func.HttpResponse(body=json.dumps(result), | ||
mimetype='application/json') |
15 changes: 15 additions & 0 deletions
15
tests/unittests/dispatcher_functions/show_context/function.json
This file contains hidden or 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,15 @@ | ||
{ | ||
"scriptFile": "__init__.py", | ||
"bindings": [ | ||
{ | ||
"type": "httpTrigger", | ||
"direction": "in", | ||
"name": "req" | ||
}, | ||
{ | ||
"type": "http", | ||
"direction": "out", | ||
"name": "$return" | ||
} | ||
] | ||
} |
This file contains hidden or 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,125 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
import os | ||
from unittest.mock import patch | ||
from azure_functions_worker import protos | ||
from azure_functions_worker import testutils | ||
from azure_functions_worker.constants import PYTHON_THREADPOOL_THREAD_COUNT | ||
|
||
|
||
class TestDispatcher(testutils.AsyncTestCase): | ||
dispatcher_funcs_dir = testutils.UNIT_TESTS_FOLDER / 'dispatcher_functions' | ||
|
||
def setUp(self): | ||
self._pre_env = dict(os.environ) | ||
|
||
def tearDown(self): | ||
os.environ.clear() | ||
os.environ.update(self._pre_env) | ||
|
||
async def test_dispatcher_sync_threadpool_default_worker(self): | ||
'''Test if the sync threadpool has maximum worker count set to 1 | ||
by default | ||
''' | ||
ctrl = testutils.start_mockhost(script_root=self.dispatcher_funcs_dir) | ||
|
||
async with ctrl as host: | ||
await self._check_if_function_is_ok(host) | ||
|
||
# Ensure the dispatcher sync threadpool count is set to 1 | ||
self.assertEqual(ctrl._worker._sync_tp_max_workers, 1) | ||
|
||
async def test_dispatcher_sync_threadpool_set_worker(self): | ||
vrdmr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'''Test if the sync threadpool maximum worker can be set | ||
''' | ||
# Configure thread pool max worker | ||
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '5'}) | ||
ctrl = testutils.start_mockhost(script_root=self.dispatcher_funcs_dir) | ||
|
||
async with ctrl as host: | ||
await self._check_if_function_is_ok(host) | ||
|
||
# Ensure the dispatcher sync threadpool count is set to 1 | ||
self.assertEqual(ctrl._worker._sync_tp_max_workers, 5) | ||
|
||
@patch('azure_functions_worker.dispatcher.logger') | ||
async def test_dispatcher_sync_threadpool_invalid_worker_count( | ||
self, | ||
mock_logger | ||
): | ||
'''Test when sync threadpool maximum worker is set to an invalid value, | ||
the host should fallback to default value 1 | ||
''' | ||
# Configure thread pool max worker to an invalid value | ||
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: 'invalid'}) | ||
ctrl = testutils.start_mockhost(script_root=self.dispatcher_funcs_dir) | ||
|
||
async with ctrl as host: | ||
await self._check_if_function_is_ok(host) | ||
|
||
# Ensure the dispatcher sync threadpool should fallback to 1 | ||
self.assertEqual(ctrl._worker._sync_tp_max_workers, 1) | ||
|
||
mock_logger.warning.assert_any_call( | ||
f'{PYTHON_THREADPOOL_THREAD_COUNT} must be an integer') | ||
|
||
@patch('azure_functions_worker.dispatcher.logger') | ||
async def test_dispatcher_sync_threadpool_below_min_setting( | ||
self, | ||
mock_logger | ||
): | ||
# Configure thread pool max worker to an invalid value | ||
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '0'}) | ||
ctrl = testutils.start_mockhost(script_root=self.dispatcher_funcs_dir) | ||
|
||
async with ctrl as host: | ||
await self._check_if_function_is_ok(host) | ||
|
||
# Ensure the dispatcher sync threadpool should fallback to 1 | ||
self.assertEqual(ctrl._worker._sync_tp_max_workers, 1) | ||
|
||
mock_logger.warning.assert_any_call( | ||
f'{PYTHON_THREADPOOL_THREAD_COUNT} must be set to a value between ' | ||
'1 and 32') | ||
|
||
@patch('azure_functions_worker.dispatcher.logger') | ||
async def test_dispatcher_sync_threadpool_exceed_max_setting( | ||
self, | ||
mock_logger | ||
): | ||
# Configure thread pool max worker to an invalid value | ||
os.environ.update({PYTHON_THREADPOOL_THREAD_COUNT: '33'}) | ||
ctrl = testutils.start_mockhost(script_root=self.dispatcher_funcs_dir) | ||
|
||
async with ctrl as host: | ||
await self._check_if_function_is_ok(host) | ||
|
||
# Ensure the dispatcher sync threadpool should fallback to 1 | ||
self.assertEqual(ctrl._worker._sync_tp_max_workers, 1) | ||
|
||
mock_logger.warning.assert_any_call( | ||
f'{PYTHON_THREADPOOL_THREAD_COUNT} must be set to a value between ' | ||
'1 and 32') | ||
|
||
async def _check_if_function_is_ok(self, host): | ||
# Ensure the function can be properly loaded | ||
func_id, load_r = await host.load_function('show_context') | ||
self.assertEqual(load_r.response.function_id, func_id) | ||
self.assertEqual(load_r.response.result.status, | ||
protos.StatusResult.Success) | ||
|
||
# Ensure the function can be properly invoked | ||
invoke_id, call_r = await host.invoke_function( | ||
'show_context', [ | ||
protos.ParameterBinding( | ||
name='req', | ||
data=protos.TypedData( | ||
http=protos.RpcHttp( | ||
method='GET' | ||
) | ||
) | ||
) | ||
]) | ||
self.assertIsNotNone(invoke_id) | ||
self.assertEqual(call_r.response.result.status, | ||
protos.StatusResult.Success) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefanushinardi - Could you please check if you want to change the name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - putting my reasoning here for future reference: it makes sense to put the PYTHON_* prefix for this as without it users might get confused and use this config for other languages