@@ -29,15 +29,20 @@ def __init__(self, apply_func, load_func=None):
29
29
self .apply_func = apply_func
30
30
self .is_local = not os .path .exists (self .FIFO_PATH )
31
31
self .load_result = None
32
+ self .loading_exception = None
32
33
33
34
def load (self ):
34
- if self .load_func :
35
- self .load_result = self .load_func ()
36
- if self .is_local :
37
- print ("loading complete" )
38
- else :
39
- print ("PIPE_INIT_COMPLETE" )
40
- sys .stdout .flush ()
35
+ try :
36
+ if self .load_func :
37
+ self .load_result = self .load_func ()
38
+ except Exception as e :
39
+ self .loading_exception = e
40
+ finally :
41
+ if self .is_local :
42
+ print ("loading complete" )
43
+ else :
44
+ print ("PIPE_INIT_COMPLETE" )
45
+ sys .stdout .flush ()
41
46
42
47
def format_data (self , request ):
43
48
if request ["content_type" ] in ["text" , "json" ]:
@@ -78,12 +83,12 @@ def format_response(self, response):
78
83
)
79
84
return response_string
80
85
81
- def write_to_pipe (self , payload ):
86
+ def write_to_pipe (self , payload , pprint = print ):
82
87
if self .is_local :
83
88
if isinstance (payload , dict ):
84
89
raise Exception (payload )
85
90
else :
86
- print (payload )
91
+ pprint (payload )
87
92
else :
88
93
if os .name == "posix" :
89
94
with open (self .FIFO_PATH , "w" ) as f :
@@ -130,22 +135,13 @@ def process_local(self, local_payload, pprint):
130
135
apply_result = self .apply_func (local_payload )
131
136
pprint (self .format_response (apply_result ))
132
137
133
- def loading_process (self , pprint ):
134
- try :
135
- self .load ()
136
- return True
137
- except Exception as e :
138
- load_error = self .create_exception (e )
139
- if self .is_local :
140
- pprint (load_error )
141
- else :
142
- self .write_to_pipe (load_error )
143
- return False
144
138
145
139
def init (self , local_payload = None , pprint = print ):
146
- loading_result = self .loading_process (pprint )
147
- if loading_result :
148
- if self .is_local and local_payload :
149
- self .process_local (local_payload , pprint )
150
- else :
151
- self .process_loop ()
140
+ self .load ()
141
+ if self .loading_exception :
142
+ load_error = self .create_exception (self .loading_exception )
143
+ self .write_to_pipe (load_error , pprint = pprint )
144
+ elif self .is_local and local_payload :
145
+ self .process_local (local_payload , pprint )
146
+ else :
147
+ self .process_loop ()
0 commit comments