I have a program that writes to stdout and possibly stderr. I want to run it from python, capturing the stdout and stderr. My code looks like:
from subprocess import * p = Popen( exe, shell=TRUE, stdout=PIPE, stderr=PIPE ) rtrncode = p.wait()
For a couple of programs, this works fine, but when I added a new one, the new one hangs forever. If I remove stdout=PIPE
, the program writes its output to the console and finishes and everything is fine. How can I determine what's causing the hang?
Using python 2.5 on Windows XP. The program does not read from stdin nor does it have any kind of user input (i.e. "hit a key").
The timeout argument is passed to Popen. communicate() . If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.
The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code.
Popen do we need to close the connection or subprocess automatically closes the connection? Usually, the examples in the official documentation are complete. There the connection is not closed. So you do not need to close most probably.
The popen() function executes the command specified by the string command. It creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from or write to the pipe.
When a pipe's buffer fills up (typically 4KB or so), the writing process stops until a reading process has read some of the data in question; but here you're reading nothing until the subprocess is done, hence the deadlock. The docs on wait
put it very clearly indeed:
Warning This will deadlock if the child process generates enough output to a stdout or stderr pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.
If you can't use communicate
for some reason, have the subprocess write to a temporary file, and then you can wait
and read that file when it's ready -- writing to a file, instead of to a pipe, does not risk deadlock.
Take a look at the docs. It states that you shouldn't use wait as it can cause a dead lock. Try using communicate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With