@@ -38,10 +38,32 @@ def filter(self, record):
38
38
return True
39
39
40
40
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
+
41
63
def call (func , event , context , environment_variables = {}):
42
64
export_variables (environment_variables )
43
-
44
- return _runner (func , event , context )
65
+ loader = FunctionLoader ( func = func )
66
+ return _runner (loader , event , context )
45
67
46
68
47
69
def run (args ):
@@ -53,17 +75,19 @@ def run(args):
53
75
args .timeout ,
54
76
invoked_function_arn = args .arn_string ,
55
77
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 )
59
83
60
- (result , err_type ) = _runner (func , e , c )
84
+ (result , err_type ) = _runner (loader , e , c )
61
85
62
86
if err_type is not None :
63
87
sys .exit (EXITCODE_ERR )
64
88
65
89
66
- def _runner (func , event , context ):
90
+ def _runner (loader , event , context ):
67
91
logger = logging .getLogger ()
68
92
69
93
logger .info ("Event: {}" .format (event ))
@@ -73,7 +97,7 @@ def _runner(func, event, context):
73
97
queue = multiprocessing .Queue ()
74
98
p = multiprocessing .Process (
75
99
target = execute_in_process ,
76
- args = (queue , func , event , context ,))
100
+ args = (queue , loader , event , context ,))
77
101
p .start ()
78
102
(result , err_type , duration ) = queue .get ()
79
103
p .join ()
@@ -94,7 +118,7 @@ def load_lib(path):
94
118
sys .path .append (os .path .abspath (path ))
95
119
96
120
97
- def load (request_id , path , function_name ):
121
+ def load_source (request_id , path , function_name ):
98
122
mod_name = 'request-' + str (request_id )
99
123
100
124
file_path = os .path .abspath (path )
@@ -142,9 +166,11 @@ def execute(func, event, context):
142
166
return result , err_type
143
167
144
168
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 ()
146
172
start_time = timeit .default_timer ()
147
- result , err_type = execute (func , event , context )
173
+ result , err_type = execute (loader . func , event , context )
148
174
end_time = timeit .default_timer ()
149
175
duration = (end_time - start_time ) * 1000
150
176
0 commit comments