Subprocess module - communicate(data) dealing with errors

P

Paul Moore

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top