Skip to content

Commit 434b935

Browse files
author
Leonhard Schick
authored
fix read stderr during command execution (#47)
Currently stderr is only read after stdout ended sending (= after the called bash command ended). With this change, stderr and stdout is read simultanously via two separate threads to make it possible that stderr is read before the command ended.
1 parent d0a7fc2 commit 434b935

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

mara_pipelines/shell.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,26 @@ def run_shell_command(command: str, log_command: bool = True):
3636
# subprocess.Popen(..) (and not custom streams without a file handle).
3737
# So in order to see be able to log the output in real-time, we have to
3838
# query the output steams of the process from to separate threads
39-
def read_process_output():
39+
def read_process_stdout():
4040
for line in process.stdout:
4141
output_lines.append(line)
4242
logger.log(line, format=logger.Format.VERBATIM)
4343

44+
def read_process_stderr():
4445
for line in process.stderr:
4546
logger.log(line, format=logger.Format.VERBATIM, is_error=True)
4647

47-
read_output_thread = threading.Thread(target=read_process_output)
48-
read_output_thread.start()
48+
read_stdout_thread = threading.Thread(target=read_process_stdout)
49+
read_stdout_thread.start()
50+
read_stderr_thread = threading.Thread(target=read_process_stderr)
51+
read_stderr_thread.start()
4952

5053
# wait until the process finishes
5154
while process.poll() is None:
5255
time.sleep(0.005)
5356

54-
read_output_thread.join()
57+
read_stdout_thread.join()
58+
read_stderr_thread.join()
5559

5660
exitcode = process.returncode
5761
if exitcode != 0:

0 commit comments

Comments
 (0)