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?
When shell=True,
args[0]
is the command to be executed andargs[1:]
are arguments passed tosh
. You want-p
andrestore_filepath
to be passed topv
notsh
. So useshell=False
. The same goes for your othersubprocess.Popen
call.Since with
shell=True
,pv
is not receiving any arguments, so it is hanging because it was still waiting for a filename. So again, the solution is to useshell=False
.Also note when replacing a shell pipeline, you should close
stdout
on the first process to allow it to receive SIGPIPE if the second process exists. This allows some programs (those that handle SIGPIPE) to more gracefully exit if the pipeline is broken.