Using subprocess to find out when a process ends
I wish to sequentially run some c scripts that fork their own processes (in a new command line window) and give the "Press any key to continue..."
when they are completed. Technically, it is a special compiler. It pops up with acommand line window and tells me whether the compile was successful or not. But that command line window forks new processes to compile, which are making this return 0 before it should..
My first attempt at this was
process = subprocess.Popen(cmd)
process.wait()
while iterating over each file. Unfortunately, this does not wait for the "Press any key to continue..." and blows up in my face. It seems that the wait()
call is passed when an internal process is completed (which I have no access to).
How can I, instead, wait for "Press any key to continue..."? It's also printing some other information before the press any key to continue line..
Currently, this is what my code is:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
completed = False
while not completed:
if开发者_运维问答 process.poll() is not None:
completed = True
print "communicating"
process.communicate("k")
print "communicated"
You can use subprocess.poll
to check the status without blocking, and subprocess.communicate
to send information to the subprocess.
精彩评论