From 5cb30290d1b69fa170c3333ac8d40218ddd8529b Mon Sep 17 00:00:00 2001 From: Aleksei Grebenkin Date: Wed, 18 Oct 2023 17:56:34 +0000 Subject: [PATCH] Lazily initialize http modules used for error handling --- awslambdaric/lambda_runtime_client.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/awslambdaric/lambda_runtime_client.py b/awslambdaric/lambda_runtime_client.py index 2066f6c..7df0f88 100644 --- a/awslambdaric/lambda_runtime_client.py +++ b/awslambdaric/lambda_runtime_client.py @@ -2,8 +2,6 @@ Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. """ -import http -import http.client import sys from awslambdaric import __version__ @@ -54,6 +52,11 @@ def __init__(self, lambda_runtime_address): self.lambda_runtime_address = lambda_runtime_address def post_init_error(self, error_response_data): + # These imports are heavy-weight. They implicitly trigger `import ssl, hashlib`. + # Importing them lazily to speed up critical path of a common case. + import http + import http.client + runtime_connection = http.client.HTTPConnection(self.lambda_runtime_address) runtime_connection.connect() endpoint = "/2018-06-01/runtime/init/error"