@@ -29,15 +29,20 @@ def __init__(self, apply_func, load_func=None):
2929 self .apply_func = apply_func
3030 self .is_local = not os .path .exists (self .FIFO_PATH )
3131 self .load_result = None
32+ self .loading_exception = None
3233
3334 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 ()
4146
4247 def format_data (self , request ):
4348 if request ["content_type" ] in ["text" , "json" ]:
@@ -78,12 +83,12 @@ def format_response(self, response):
7883 )
7984 return response_string
8085
81- def write_to_pipe (self , payload ):
86+ def write_to_pipe (self , payload , pprint = print ):
8287 if self .is_local :
8388 if isinstance (payload , dict ):
8489 raise Exception (payload )
8590 else :
86- print (payload )
91+ pprint (payload )
8792 else :
8893 if os .name == "posix" :
8994 with open (self .FIFO_PATH , "w" ) as f :
@@ -130,22 +135,13 @@ def process_local(self, local_payload, pprint):
130135 apply_result = self .apply_func (local_payload )
131136 pprint (self .format_response (apply_result ))
132137
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
144138
145139 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