[Chicago] capturing output from subprocesses

Ian Bicking ianb at colorstudy.com
Tue Nov 8 01:20:18 CET 2005

Noel Thomas Taylor wrote:
> I just joined the list and hope to come to the meeting this Thursday. If I 
> can't, however, I wonder if someone could help me with a very tricky 
> problem...
> A couple of months ago I started trying to find a way to launch a
> subprocess in python that would allow the parent to capture the stdout
> and stderr of the child, time the duration of the child, and abort the
> child if it had failed to produce output after 'n' seconds. Also, the
> solution should perform consistently across all Unices (Windows doesn't
> matter) and if the child must be aborted, whatever output it produced up
> to that point should still be captured.
> There are several solutions out there, but most do not capture the output 
> of the child in real time. You always have to wait for the child to 
> terminate until you can see any of its output, and consequently if the 
> child must be aborted because it gets stuck, you lose whatever output it 
> generated before you killed it.

The subprocess module can do this.  It's a little funny, because it 
doesn't allow streaming using proc.communicate(), and I find it 
difficult to do normal piping because of the blocking.

But if you look at the bottom of this file there's a function 
proc_communicate allows you to communicate using Python file-like objects:


It's basically a slight rewrite of Proc.communicate().  There's already 
some patches filed for this issue, so hopefully subprocess in Python 2.5 
will make this easier.

Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org

More information about the Chicago mailing list