By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,676 Members | 2,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,676 IT Pros & Developers. It's quick & easy.

Subprocess module - communicate(data) dealing with errors

P: n/a
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

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,
Most conversations are simply monologues delivered in the presence of
witnesses. -- Margaret Millar
Nov 21 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.