|
26 | 26 | PYTHON_THREADPOOL_THREAD_COUNT_MAX_37,
|
27 | 27 | PYTHON_THREADPOOL_THREAD_COUNT_MIN,
|
28 | 28 | PYTHON_ENABLE_DEBUG_LOGGING, SCRIPT_FILE_NAME,
|
29 |
| - PYTHON_LANGUAGE_RUNTIME) |
| 29 | + PYTHON_LANGUAGE_RUNTIME, PYTHON_LOAD_FUNCTIONS_INIT) |
30 | 30 | from .extension import ExtensionManager
|
31 | 31 | from .logging import disable_console_logging, enable_console_logging
|
32 | 32 | from .logging import enable_debug_logging_recommendation
|
@@ -288,6 +288,10 @@ async def _handle__worker_init_request(self, request):
|
288 | 288 | if not DependencyManager.is_in_linux_consumption():
|
289 | 289 | DependencyManager.prioritize_customer_dependencies()
|
290 | 290 |
|
| 291 | + if DependencyManager.is_in_linux_consumption() \ |
| 292 | + and is_envvar_true(PYTHON_LOAD_FUNCTIONS_INIT): |
| 293 | + import azure.functions # NoQA |
| 294 | + |
291 | 295 | return protos.StreamingMessage(
|
292 | 296 | request_id=self.request_id,
|
293 | 297 | worker_init_response=protos.WorkerInitResponse(
|
@@ -450,6 +454,10 @@ async def _handle__invocation_request(self, request):
|
450 | 454 | shmem_mgr=self._shmem_mgr)
|
451 | 455 |
|
452 | 456 | fi_context = self._get_context(invoc_request, fi.name, fi.directory)
|
| 457 | + |
| 458 | + # Use local thread storage to store the invocation ID |
| 459 | + # for a customer's threads |
| 460 | + fi_context.thread_local_storage.invocation_id = invocation_id |
453 | 461 | if fi.requires_context:
|
454 | 462 | args['context'] = fi_context
|
455 | 463 |
|
@@ -525,10 +533,11 @@ async def _handle__function_environment_reload_request(self, request):
|
525 | 533 | func_env_reload_request = \
|
526 | 534 | request.function_environment_reload_request
|
527 | 535 |
|
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 |
| 536 | + if not is_envvar_true(PYTHON_LOAD_FUNCTIONS_INIT): |
| 537 | + # Import before clearing path cache so that the default |
| 538 | + # azure.functions modules is available in sys.modules for |
| 539 | + # customer use |
| 540 | + import azure.functions # NoQA |
532 | 541 |
|
533 | 542 | # Append function project root to module finding sys.path
|
534 | 543 | if func_env_reload_request.function_app_directory:
|
@@ -660,7 +669,7 @@ def _get_context(invoc_request: protos.InvocationRequest, name: str,
|
660 | 669 |
|
661 | 670 | return bindings.Context(
|
662 | 671 | name, directory, invoc_request.invocation_id,
|
663 |
| - trace_context, retry_context) |
| 672 | + _invocation_id_local, trace_context, retry_context) |
664 | 673 |
|
665 | 674 | @disable_feature_by(constants.PYTHON_ROLLBACK_CWD_PATH)
|
666 | 675 | def _change_cwd(self, new_cwd: str):
|
@@ -728,12 +737,12 @@ def _create_sync_call_tp(
|
728 | 737 | def _run_sync_func(self, invocation_id, context, func, params):
|
729 | 738 | # This helper exists because we need to access the current
|
730 | 739 | # invocation_id from ThreadPoolExecutor's threads.
|
731 |
| - _invocation_id_local.v = invocation_id |
| 740 | + context.thread_local_storage.invocation_id = invocation_id |
732 | 741 | try:
|
733 | 742 | return ExtensionManager.get_sync_invocation_wrapper(context,
|
734 | 743 | func)(params)
|
735 | 744 | finally:
|
736 |
| - _invocation_id_local.v = None |
| 745 | + context.thread_local_storage.invocation_id = None |
737 | 746 |
|
738 | 747 | async def _run_async_func(self, context, func, params):
|
739 | 748 | return await ExtensionManager.get_async_invocation_wrapper(
|
@@ -837,7 +846,7 @@ def get_current_invocation_id() -> Optional[str]:
|
837 | 846 | if task_invocation_id is not None:
|
838 | 847 | return task_invocation_id
|
839 | 848 |
|
840 |
| - return getattr(_invocation_id_local, 'v', None) |
| 849 | + return getattr(_invocation_id_local, 'invocation_id', None) |
841 | 850 |
|
842 | 851 |
|
843 | 852 | _invocation_id_local = threading.local()
|
0 commit comments