Reputation: 31
I'm running a tool via Python
in cmd
. For each sample in a given directory I want that tool to do something. However, when I use process = subprocess.Popen(command)
in the loop, the commands does not wait untill its finished, resulting in 10 prompts at once. And when I use subprocess.Popen(command, stdout=subprocess.PIPE)
the command remains black and I can't see the progress, although it does wait untill the command is finished.
Does anyone know a way how to call an external tool via Python
in cmd
, that does wait untill the command is finished and thats able to show the progress of the tool in the cmd
?
#main.py
for sample in os.listdir(os.getcwd()):
if ".fastq" in sample and '_R1_' in sample and "Temp" not in sample:
print time.strftime("%H:%M:%S")
DNA_Bowtie2.DNA_Bowtie2(os.getcwd()+'\\'+sample+'\\'+sample)
#DNA_Bowtie2.py
# Run Bowtie2 command and wait for process to be finished.
process = subprocess.Popen(command, stdout=subprocess.PIPE)
process.wait()
process.stdout.read()
Edit: command = a perl or java command. With above make-up I cannot see tool output since the prompt (perl window, or java window) remains black.
Upvotes: 2
Views: 506
Reputation: 91039
The order is important here: first read the output, then wait.
If you do it this way:
process.wait()
process.stdout.read()
you can experience a deadlock if the pipe buffer is completely full: the subprocess blocks on waiting on stdout and never reaches the end, your program blocks on wait()
and never reaches the read()
.
Do instead
process.stdout.read()
process.wait()
which will read until EOF.
This holds for if you want the stdout of the process at all.
If you don't want that, you should omit the stdout=PIPE
stuff. Then the output is directed into that prompt window. Then you can omit process.stdout.read()
as well.
Normally, the process.wait()
should then prevent that 10 instances run at once. If that doesn't work, I don't know why not...
Upvotes: 0
Reputation:
It seems like your subprocess forks otherwise there is no way the wait() would return before the process has finished.
Upvotes: 1