Python内执行cmd命令

def log_popen_pipe(process, stdfile):
    while process.poll() is None:
        if stdfile == process.stdout:
            out_msg = stdfile.readline().decode("utf-8")
            if out_msg:
                sys.stdout.write("[stdout]" + out_msg)
                sys.stdout.flush()
        else:
            err_msg = stdfile.readline().decode("utf-8")
            if err_msg:
                sys.stderr.write("[stderr]" + err_msg)
                sys.stderr.flush()
    # Write the rest from the buffer
    if stdfile == process.stdout:
        sys.stdout.write(stdfile.read().decode("utf-8"))
    else:
        sys.stderr.write(stdfile.read().decode("utf-8"))


def run_process(cmd, env=None):
    print("run_process = ", " ".join(cmd))

    with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) as proc:
        with ThreadPoolExecutor(max_workers=2) as pool:
            stdout = pool.submit(log_popen_pipe, proc, proc.stdout)
            stderr = pool.submit(log_popen_pipe, proc, proc.stderr)
            stdout.result()
            stderr.result()