Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Subprocess module - communicate(data) dealing with errors

Reply
Thread Tools

Subprocess module - communicate(data) dealing with errors

 
 
Paul Moore
Guest
Posts: n/a
 
      11-21-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to avoid Out of Memory Errors when dealing with a large XML file? Saqib Ali Perl Misc 2 01-14-2011 09:31 PM
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
When exceptions aren't enough: Dealing with runtime errors. Aaron W. LaFramboise C++ 4 07-25-2005 04:58 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57