views:

54

answers:

1

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"
+1  A: 

You can use subprocess.poll to check the status without blocking, and subprocess.communicate to send information to the subprocess.

Wayne Werner
If it's giving me "Press any key to continue..." i thought it hadn't returned any value yet? Would this not make subprocess.poll kind of useless?
hungrydude
Yeah, this doesn't seem to be waiting for it to print "Press any key to continue..."
hungrydude
Try just using communicate - of course this assumes that `stdout` and `stdin` have been tied to the `subprocess.PIPE`, so you'll need to change it to `.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)`
Wayne Werner
I've updated the question with what I have. It seems to lock at the communicate() command. I have also tried it without an argument to try to get the stdout from it, but no dice.
hungrydude
remove/comment your while loop and see if that works.
Wayne Werner