@@ -38,10 +38,32 @@ def filter(self, record):
3838 return True
3939
4040
41+ class FunctionLoader ():
42+ def __init__ (self ,
43+ request_id = None ,
44+ source = None ,
45+ function_name = None ,
46+ library_path = None ,
47+ func = None ):
48+ self .request_id = request_id
49+ self .source = source
50+ self .function_name = function_name
51+ self .library_path = library_path
52+
53+ self .func = func
54+
55+ def load (self ):
56+ if self .library_path is not None :
57+ load_lib (self .library_path )
58+
59+ self .func = load_source (
60+ self .request_id , self .source , self .function_name )
61+
62+
4163def call (func , event , context , environment_variables = {}):
4264 export_variables (environment_variables )
43-
44- return _runner (func , event , context )
65+ loader = FunctionLoader ( func = func )
66+ return _runner (loader , event , context )
4567
4668
4769def run (args ):
@@ -53,17 +75,19 @@ def run(args):
5375 args .timeout ,
5476 invoked_function_arn = args .arn_string ,
5577 function_version = args .version_name )
56- if args .library is not None :
57- load_lib (args .library )
58- func = load (c .aws_request_id , args .file , args .function )
78+ loader = FunctionLoader (
79+ request_id = c .aws_request_id ,
80+ source = args .file ,
81+ function_name = args .function ,
82+ library_path = args .library )
5983
60- (result , err_type ) = _runner (func , e , c )
84+ (result , err_type ) = _runner (loader , e , c )
6185
6286 if err_type is not None :
6387 sys .exit (EXITCODE_ERR )
6488
6589
66- def _runner (func , event , context ):
90+ def _runner (loader , event , context ):
6791 logger = logging .getLogger ()
6892
6993 logger .info ("Event: {}" .format (event ))
@@ -73,7 +97,7 @@ def _runner(func, event, context):
7397 queue = multiprocessing .Queue ()
7498 p = multiprocessing .Process (
7599 target = execute_in_process ,
76- args = (queue , func , event , context ,))
100+ args = (queue , loader , event , context ,))
77101 p .start ()
78102 (result , err_type , duration ) = queue .get ()
79103 p .join ()
@@ -94,7 +118,7 @@ def load_lib(path):
94118 sys .path .append (os .path .abspath (path ))
95119
96120
97- def load (request_id , path , function_name ):
121+ def load_source (request_id , path , function_name ):
98122 mod_name = 'request-' + str (request_id )
99123
100124 file_path = os .path .abspath (path )
@@ -142,9 +166,11 @@ def execute(func, event, context):
142166 return result , err_type
143167
144168
145- def execute_in_process (queue , func , event , context ):
169+ def execute_in_process (queue , loader , event , context ):
170+ if loader .func is None :
171+ loader .load ()
146172 start_time = timeit .default_timer ()
147- result , err_type = execute (func , event , context )
173+ result , err_type = execute (loader . func , event , context )
148174 end_time = timeit .default_timer ()
149175 duration = (end_time - start_time ) * 1000
150176
0 commit comments