473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Subprocess module - communicate(data) dealing with errors

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
Nov 21 '06 #1
0 1104

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Art | last post by:
Hi folks, I'm writing a traditional desktop app using VB.NET and am stumbling over what seems like a very basic question: My app does not need to be connected to a server or another computer....
3
by: =?ISO-8859-1?Q?Gregory_Pi=F1ero?= | last post by:
Hi Python Experts, I hope I can explain this right. I'll try. Background: I have a module that I leave running in a server role. It has a module which has data in it that can change. So...
3
by: Lee | last post by:
Has anyone ran into this problem? I've done extensive googling and research and I cannot seem to find the answer. I downloaded the source for 2.5.1 from python.org compiled and installed it on a...
3
by: Peter J. Bismuti | last post by:
How do you define a "module data member" (I want to understand out how this works before making converting to a Class)? Right now I'm defining variables in a module that get put into the global...
2
by: Macias | last post by:
Hi, Please tell me how I can exchange data between two forms. My main form is Form1, and I display a new form something like this: private void Settings_Click(object sender, EventArgs e) { using...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
0
by: rkmr.em | last post by:
there was a mistake in my prev mail.. it is not able to successfully import the module. abcde is in directory /home/mark/work/proj1, but it is looking for it in /home/mark from where i am...
0
by: Gabriel Genellina | last post by:
En Wed, 19 Nov 2008 20:06:37 -0200, Yann Vonder <yann.vonderscher@gmail.comescribió: If you put tmod in both libraries, you'll have two copies of it, and two copies of the data. Can't you join...
1
by: chanshaw | last post by:
Alright here is the issue: We have a corperate network which all professors can use, and then we have a student network for the students. Now we bought a software that allows remote monitoring of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.