Skip to content

Refactor conditional loading of ThreadPoolExecutor #125

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 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions awslambdaric/lambda_runtime_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class LambdaRuntimeClient(object):
def __init__(self, lambda_runtime_address, use_thread_for_polling_next=False):
self.lambda_runtime_address = lambda_runtime_address
self.use_thread_for_polling_next = use_thread_for_polling_next
if self.use_thread_for_polling_next:
# Conditionally import only for the case when TPE is used in this class.
from concurrent.futures import ThreadPoolExecutor

# Not defining symbol as global to avoid relying on TPE being imported unconditionally.
self.ThreadPoolExecutor = ThreadPoolExecutor

def post_init_error(self, error_response_data):
# These imports are heavy-weight. They implicitly trigger `import ssl, hashlib`.
Expand All @@ -74,9 +80,8 @@ def wait_next_invocation(self):
# which can then process signals.
if self.use_thread_for_polling_next:
try:
from concurrent.futures import ThreadPoolExecutor

with ThreadPoolExecutor(max_workers=1) as executor:
# TPE class is supposed to be registered at construction time and be ready to use.
with self.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(runtime_client.next)
response_body, headers = future.result()
except Exception as e:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,11 @@ def raise_exception_handler(json_input, lambda_context):

self.assertEqual(mock_stdout.getvalue(), error_logs)

@patch("sys.stdout", new_callable=StringIO)
# The order of patches matter. Using MagicMock resets sys.stdout to the default.
@patch("importlib.import_module")
@patch("sys.stdout", new_callable=StringIO)
def test_handle_event_request_fault_exception_logging_syntax_error(
self, mock_import_module, mock_stdout
self, mock_stdout, mock_import_module
):
try:
eval("-")
Expand Down