Subprocess module - communicate(data) dealing with errors

Paul Moore pf_moore at yahoo.co.uk
Tue Nov 21 23:51:10 CET 2006


I've just hit an annoying corner case in the subprocess module. I'm
trying to run a process, pass it some input and capture output and
error data:

    cmd = ['tr', 'a-z', 'A-Z']
    p = Popen(cmd, stdin=PIPE, stout=PIPE, stderr=PIPE)
    out, err = p.communicate("Hello, world!")

This works really well, *except* if cmd has an error (for example, add
an extra argument). In that case, the subprocess finishes before the
communicate() call, and so the communicate() call fails with an
IOError writing to the subprocess' stdin handle.

The trouble is, as far as I can tell, this is a race condition - I can
check (with poll()) if the command has terminated before I try
communicate(), but there's still a chance it terminates between the
poll and the communicate.

I'd really like to make this as near to foolproof as I can - this is
to go into a server process, and tracebacks aren't really suitable
output... :-) Can anyone suggest a way I can code defensively round
this?

In case it matters, I'm running on Windows - I don't know enough about
POSIX to say if the same issue occurs there.

Thanks for any suggestions,
Paul.
-- 
Most conversations are simply monologues delivered in the presence of
witnesses. -- Margaret Millar



More information about the Python-list mailing list