Python subprocess threading with conole log not immediately updating

25 Views Asked by At

I'd like to write a function where I can input any CLI command, which then runs inside it's own subprocess, and which logs it's output to a file and to the terminal.

The execution of a command can take hours, so the python file should be able to spawn mutiple processes at the same time, without them interfering, or writing to the same log file.

Current problem of my code: the last line which is outputted by the progress is not printed until the whole thing finishes, also the line updates continously as it shows progress bar and some other stats.!


def read_output(stream, logfile):
    with open(logfile, "a") as log_file:
        for line in stream:
            log_file.write(line)
            log_file.flush()  
            print(prefix, line, end='') 

def run_subprocess(command):
    try:
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
        with open(log_file, "w") as logfile:  # Open log file in write mode to clear its contents
            logfile.write("started cmd\n")

        # Thread for stoud and one for stderrr
        stdout_thread = threading.Thread(target=read_output, args=(process.stdout, log_file))
        stderr_thread = threading.Thread(target=read_output, args=(process.stderr, log_file))
        stdout_thread.start()
        stderr_thread.start()

        process.wait()
        if process.returncode != 0:
            raise subprocess.CalledProcessError(process.returncode, command)
    except subprocess.CalledProcessError as e:
        print(f"Command '{e.cmd}' returned non-zero exit status {e.returncode}.")

def execute_command(command):
    thread = threading.Thread(target=run_subprocess, args=(command,))
    thread.start()
    print("starting Thread")

execute_command(command)

Expected: expected

Reality: reality

Thanks for any help!

0

There are 0 best solutions below