From 81499333bc45c4f14ad3b91c7da4191dbb13b7e3 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Fri, 6 Dec 2019 11:18:34 +0900 Subject: [PATCH 1/4] Use different import. --- lambda_local/main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lambda_local/main.py b/lambda_local/main.py index 1f5f83f..90e461e 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -102,7 +102,17 @@ def load(request_id, path, function_name): file_directory = os.path.dirname(file_path) sys.path.append(file_directory) - mod = imp.load_source(mod_name, path) + if sys.version_info.major == 2: + mod = imp.load_source(mod_name, path) + elif sys.version_info.major == 3 and sys.version_info.minor >= 5: + import importlib + spec = importlib.util.spec_from_file_location(mod_name, path) + mod = importlib.util.module_from_spec(spec) + sys.modules[mod_name] = mod + spec.loader.exec_module(mod) + else: + raise Exception("unsupported python version") + func = getattr(mod, function_name) return func From 7868e46cd6d0b7071c41d8032a65049db41bb75a Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Fri, 6 Dec 2019 11:55:25 +0900 Subject: [PATCH 2/4] Move import. --- lambda_local/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambda_local/main.py b/lambda_local/main.py index 90e461e..0f248cd 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -3,7 +3,6 @@ Licensed under MIT. ''' -import imp import sys import traceback import json @@ -103,6 +102,7 @@ def load(request_id, path, function_name): sys.path.append(file_directory) if sys.version_info.major == 2: + import imp mod = imp.load_source(mod_name, path) elif sys.version_info.major == 3 and sys.version_info.minor >= 5: import importlib From 62563884cbc65c977a223545e2d1d21e5e96f03f Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Tue, 10 Dec 2019 11:42:27 +0900 Subject: [PATCH 3/4] Load function in child process. --- lambda_local/main.py | 48 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/lambda_local/main.py b/lambda_local/main.py index 0f248cd..b3645d2 100644 --- a/lambda_local/main.py +++ b/lambda_local/main.py @@ -38,10 +38,32 @@ def filter(self, record): return True +class FunctionLoader(): + def __init__(self, + request_id=None, + source=None, + function_name=None, + library_path=None, + func=None): + self.request_id = request_id + self.source = source + self.function_name = function_name + self.library_path = library_path + + self.func = func + + def load(self): + if self.library_path is not None: + load_lib(self.library_path) + + self.func = load_source( + self.request_id, self.source, self.function_name) + + def call(func, event, context, environment_variables={}): export_variables(environment_variables) - - return _runner(func, event, context) + loader = FunctionLoader(func=func) + return _runner(loader, event, context) def run(args): @@ -53,17 +75,19 @@ def run(args): args.timeout, invoked_function_arn=args.arn_string, function_version=args.version_name) - if args.library is not None: - load_lib(args.library) - func = load(c.aws_request_id, args.file, args.function) + loader = FunctionLoader( + request_id=c.aws_request_id, + source=args.file, + function_name=args.function, + library_path=args.library) - (result, err_type) = _runner(func, e, c) + (result, err_type) = _runner(loader, e, c) if err_type is not None: sys.exit(EXITCODE_ERR) -def _runner(func, event, context): +def _runner(loader, event, context): logger = logging.getLogger() logger.info("Event: {}".format(event)) @@ -73,7 +97,7 @@ def _runner(func, event, context): queue = multiprocessing.Queue() p = multiprocessing.Process( target=execute_in_process, - args=(queue, func, event, context,)) + args=(queue, loader, event, context,)) p.start() (result, err_type, duration) = queue.get() p.join() @@ -94,7 +118,7 @@ def load_lib(path): sys.path.append(os.path.abspath(path)) -def load(request_id, path, function_name): +def load_source(request_id, path, function_name): mod_name = 'request-' + str(request_id) file_path = os.path.abspath(path) @@ -142,9 +166,11 @@ def execute(func, event, context): return result, err_type -def execute_in_process(queue, func, event, context): +def execute_in_process(queue, loader, event, context): + if loader.func is None: + loader.load() start_time = timeit.default_timer() - result, err_type = execute(func, event, context) + result, err_type = execute(loader.func, event, context) end_time = timeit.default_timer() duration = (end_time - start_time) * 1000 From 25c4362a0e10b9967b35952d44749eeefaebc262 Mon Sep 17 00:00:00 2001 From: YANG Xudong Date: Tue, 10 Dec 2019 12:05:53 +0900 Subject: [PATCH 4/4] Update metadata and bump version. --- setup.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 26f49dd..fc9290c 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ def run_tests(self): sys.exit(pytest.main(self.test_args)) -version = "0.1.11" +version = "0.1.12" TEST_REQUIRE = ['pytest'] if sys.version_info[0] == 2: @@ -39,11 +39,12 @@ def run_tests(self): 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', 'License :: OSI Approved :: MIT License' ], keywords="AWS Lambda", author="YANG Xudong", - author_email="xudong.yang@hde.co.jp", + author_email="xudong.yang@hennge.com", url="https://github.com/HDE/python-lambda-local", license="MIT", packages=find_packages(exclude=['examples', 'tests']),