@@ -164,6 +164,12 @@ def terminate():
164
164
rselect = [nodejs .stdout , nodejs .stderr ] # type: List[BytesIO]
165
165
wselect = [nodejs .stdin ] # type: List[BytesIO]
166
166
167
+ PROCESS_FINISHED_STR = "r1cepzbhUTxtykz5XTC4\n "
168
+
169
+ def process_finished ():
170
+ return stdout_buf .getvalue ().decode ().endswith (PROCESS_FINISHED_STR ) and \
171
+ stderr_buf .getvalue ().decode ().endswith (PROCESS_FINISHED_STR )
172
+
167
173
# On windows system standard input/output are not handled properly by select module
168
174
# (modules like pywin32, msvcrt, gevent don't work either)
169
175
if sys .platform == 'win32' :
@@ -214,7 +220,9 @@ def get_error(error_queue):
214
220
error_thread .daemon = True
215
221
error_thread .start ()
216
222
217
- while (len (wselect ) + len (rselect )) > 0 :
223
+ finished = False
224
+
225
+ while not finished and tm .isAlive ():
218
226
try :
219
227
if nodejs .stdin in wselect :
220
228
if not input_queue .empty ():
@@ -227,41 +235,35 @@ def get_error(error_queue):
227
235
228
236
if nodejs .stderr in rselect :
229
237
if not error_queue .empty ():
230
- stderr_buf .write (error_queue .get ())
238
+ stderr_buf .write (error_queue .get ())
231
239
232
- if stdout_buf . getvalue (). endswith ( " \n " . encode () ):
233
- rselect = []
240
+ if process_finished () and error_queue . empty () and output_queue . empty ( ):
241
+ finished = True
234
242
no_more_output .release ()
235
243
no_more_error .release ()
236
244
except OSError as e :
237
245
break
238
246
239
247
else :
240
- while ( len ( wselect ) + len ( rselect )) > 0 :
248
+ while not ( process_finished () or not tm . isAlive ()) :
241
249
rready , wready , _ = select .select (rselect , wselect , [])
242
250
try :
243
251
if nodejs .stdin in wready :
244
252
b = stdin_buf .read (select .PIPE_BUF )
245
253
if b :
246
254
os .write (nodejs .stdin .fileno (), b )
247
- else :
248
- wselect = []
249
255
for pipes in ((nodejs .stdout , stdout_buf ), (nodejs .stderr , stderr_buf )):
250
256
if pipes [0 ] in rready :
251
257
b = os .read (pipes [0 ].fileno (), select .PIPE_BUF )
252
258
if b :
253
259
pipes [1 ].write (b )
254
- else :
255
- rselect .remove (pipes [0 ])
256
- if stdout_buf .getvalue ().endswith ("\n " .encode ()):
257
- rselect = []
258
260
except OSError as e :
259
261
break
260
262
tm .cancel ()
261
263
262
264
stdin_buf .close ()
263
- stdoutdata = stdout_buf .getvalue ()
264
- stderrdata = stderr_buf .getvalue ()
265
+ stdoutdata = stdout_buf .getvalue ()[: - len ( PROCESS_FINISHED_STR ) - 1 ]
266
+ stderrdata = stderr_buf .getvalue ()[: - len ( PROCESS_FINISHED_STR ) - 1 ]
265
267
266
268
def fn_linenum (): # type: () -> Text
267
269
lines = fn .splitlines ()
0 commit comments