[Python] Getting pv output with subprocess

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a script to automate database imports in MySQL. I'm trying to write code that displays the output of pv as the database is imported:

    pv = subprocess.Popen(
    ["pv", "-f", restore_filepath],
    bufsize=1,
    stderr=subprocess.PIPE,
    stdout=subprocess.PIPE,
    universal_newlines=True,
    shell=True,
    )
    subprocess.Popen(
    [
    "mysql",
    "-u{}".format(db_user),
    "-p{}".format(db_pass),
    db_name,
    ],
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.DEVNULL,
    stdin=pv.stdout,
    )
    for line in pv.stderr:
    print("hello")
    sys.stdout.write("\r" + line.rstrip("\n"))
    sys.stdout.flush()


    This is based on the code in this question, but it is not working for me. The hello never even gets printed, even if I comment out the other lines in the for loop - for line in pv.stderr is blocking and I don't know why. It never unblocks, so even once the process has completed, the program is still stuck - I have to kill it.

    What am I doing wrong that is causing for line in pv.stderr to block?

    Continue reading...

Compartilhe esta Página