Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using subprocess.Popen(), stderr and stdout have no output

I'm using Python to automate an SVN commit, and I want to write the SVN command's output to a log file. The code that I have can make SVN run, but the problem is that on a successful commit, the subprocess invocation does not return any output for my log.

When I run SVN manually, by comparison, I get output that shows the progress of the command and shows which files are being committed. That's what I want in my log file. Is SVN outputting that data to a buffer than stdout or stderr? How can I capture that data for my log?

Here's the code I'm using:

cmd = "svn commit --non-interactive --no-auth-cache -m 'Automatic commit' ./"
process = subprocess.Popen(cmd,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           universal_newlines=True)
result = process.wait()

# Output
out = process.stdout.read()
err = process.stderr.read()

When I run this code and the commit is successful, the out and err variables are both empty.

like image 488
Jeremy Gillick Avatar asked May 02 '12 00:05

Jeremy Gillick


1 Answers

Don't use wait() when you are using PIPE. Use communicate()

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
          stderr=subprocess.PIPE, universal_newlines=True)

out, err = process.communicate()

From the subprocess docs:

Warning
Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

like image 109
jdi Avatar answered Sep 20 '22 08:09

jdi