Skip to content

Commit

Permalink
Merge pull request #64 from HDE/master
Browse files Browse the repository at this point in the history
Release v0.1.12
  • Loading branch information
yxd-hde authored Dec 10, 2019
2 parents 3aa3309 + 25c4362 commit 9e87bfb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
62 changes: 49 additions & 13 deletions lambda_local/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under MIT.
'''

import imp
import sys
import traceback
import json
Expand Down Expand Up @@ -39,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):
Expand All @@ -54,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))
Expand All @@ -74,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()
Expand All @@ -95,14 +118,25 @@ 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)
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:
import imp
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

Expand Down Expand Up @@ -132,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

Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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']),
Expand Down

0 comments on commit 9e87bfb

Please sign in to comment.