Skip to content

Commit 041fbce

Browse files
gavin-aguiarGavin AguiarYunchuWangpdthummar
authored
Move importing azfunc to init request (#1170)
* Move importing azfunc to init request * Added tests * Flake8 test fix --------- Co-authored-by: Gavin Aguiar <gavin@GavinPC> Co-authored-by: peterstone2017 <[email protected]> Co-authored-by: pdthummar <[email protected]>
1 parent a858fbf commit 041fbce

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

azure_functions_worker/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"""
3131
UNIX_SHARED_MEMORY_DIRECTORIES = "FUNCTIONS_UNIX_SHARED_MEMORY_DIRECTORIES"
3232

33+
# Flag to enable loading functions at init request
34+
PYTHON_LOAD_FUNCTIONS_INIT = "PYTHON_LOAD_FUNCTIONS_INIT"
35+
3336
# Setting Defaults
3437
PYTHON_THREADPOOL_THREAD_COUNT_DEFAULT = 1
3538
PYTHON_THREADPOOL_THREAD_COUNT_MIN = 1

azure_functions_worker/dispatcher.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
PYTHON_THREADPOOL_THREAD_COUNT_MAX_37,
2727
PYTHON_THREADPOOL_THREAD_COUNT_MIN,
2828
PYTHON_ENABLE_DEBUG_LOGGING, SCRIPT_FILE_NAME,
29-
PYTHON_LANGUAGE_RUNTIME)
29+
PYTHON_LANGUAGE_RUNTIME, PYTHON_LOAD_FUNCTIONS_INIT)
3030
from .extension import ExtensionManager
3131
from .logging import disable_console_logging, enable_console_logging
3232
from .logging import enable_debug_logging_recommendation
@@ -288,6 +288,10 @@ async def _handle__worker_init_request(self, request):
288288
if not DependencyManager.is_in_linux_consumption():
289289
DependencyManager.prioritize_customer_dependencies()
290290

291+
if DependencyManager.is_in_linux_consumption() \
292+
and is_envvar_true(PYTHON_LOAD_FUNCTIONS_INIT):
293+
import azure.functions # NoQA
294+
291295
return protos.StreamingMessage(
292296
request_id=self.request_id,
293297
worker_init_response=protos.WorkerInitResponse(
@@ -525,10 +529,11 @@ async def _handle__function_environment_reload_request(self, request):
525529
func_env_reload_request = \
526530
request.function_environment_reload_request
527531

528-
# Import before clearing path cache so that the default
529-
# azure.functions modules is available in sys.modules for
530-
# customer use
531-
import azure.functions # NoQA
532+
if not is_envvar_true(PYTHON_LOAD_FUNCTIONS_INIT):
533+
# Import before clearing path cache so that the default
534+
# azure.functions modules is available in sys.modules for
535+
# customer use
536+
import azure.functions # NoQA
532537

533538
# Append function project root to module finding sys.path
534539
if func_env_reload_request.function_app_directory:

tests/unittests/test_dispatcher.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,36 @@ async def test_dispatcher_functions_metadata_request_legacy_fallback(self):
599599
self.assertTrue(r.response.use_default_metadata_indexing)
600600
self.assertEqual(r.response.result.status,
601601
protos.StatusResult.Success)
602+
603+
604+
class TestDispatcherLoadFunctionInInitRequest(testutils.AsyncTestCase):
605+
606+
def setUp(self):
607+
self._ctrl = testutils.start_mockhost(
608+
script_root=DISPATCHER_FUNCTIONS_DIR)
609+
self._pre_env = dict(os.environ)
610+
self.mock_version_info = patch(
611+
'azure_functions_worker.dispatcher.sys.version_info',
612+
SysVersionInfo(3, 9, 0, 'final', 0))
613+
self.mock_version_info.start()
614+
615+
def tearDown(self):
616+
os.environ.clear()
617+
os.environ.update(self._pre_env)
618+
self.mock_version_info.stop()
619+
620+
async def test_dispatcher_load_azfunc_in_init(self):
621+
"""Test if the dispatcher's log can be flushed out during worker
622+
initialization
623+
"""
624+
os.environ.update({"CONTAINER_NAME": 'test',
625+
"PYTHON_LOAD_FUNCTIONS_INIT": "1"})
626+
async with self._ctrl as host:
627+
r = await host.init_worker('4.15.1')
628+
self.assertEqual(
629+
len([log for log in r.logs if log.message.startswith(
630+
'Received WorkerInitRequest'
631+
)]),
632+
1
633+
)
634+
self.assertIn("azure.functions", sys.modules)

0 commit comments

Comments
 (0)