Right now I have some code that uses Popen.communicate()
from subprocess (setting stdin=PIPE
and stderr=PIPE
) to run a command and capture both stderr and stdout.
The problem is that communicate()
not only waits for the command to exit, it waits for stdout and stderr to be closed. The command I'm running spawns a child process which keeps stderr open, so even though the command is finished running (and shows as "defunct" in ps) communicate()
is still hung.
I want to only wait for the command to finish without waiting on stderr/stdout. But I still want to capture any stderr/stdout output given while the command was running. The documentation for wait()
is accompanied by a red box with a disclaimer:
This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.
Obviously, I also want to avoid deadlocks.
What is the right way to accomplish this task?