473,561 Members | 3,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subprocess -- broken pipe error

Hi,

Can someone explain what a broken pipe is? The following produces a
broken pipe error:

----------
import subprocess as sub

p = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE )

print p.stdout.read()
#outputs the files correctly

p.stdin.write(" ls\n")
#IOError: [Errno 32] Broken pipe
-----------

Jul 2 '07 #1
11 21199
On Jul 2, 1:12 pm, 7stud <bbxx789_0...@y ahoo.comwrote:
Hi,

Can someone explain what a broken pipe is? The following produces a
broken pipe error:

----------
import subprocess as sub

p = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE )

print p.stdout.read()
#outputs the files correctly

p.stdin.write(" ls\n")
#IOError: [Errno 32] Broken pipe
-----------
You are seeing this error because sub.Popen closes both stdin and
stdout once the subprocess terminates (which it must have done for
p.stdout.read() to return a result).

Consequently you are trying to write to a pipeline whose reader has
already closed it, hence the error message.

regards
Steve

Jul 2 '07 #2
On Jul 2, 11:32 am, "holden...@gmai l.com" <holden...@gmai l.comwrote:
On Jul 2, 1:12 pm, 7stud <bbxx789_0...@y ahoo.comwrote:
Hi,
Can someone explain what a broken pipe is? The following produces a
broken pipe error:
----------
import subprocess as sub
p = sub.Popen(["ls", "-al", "../"], stdin=sub.PIPE, stdout=sub.PIPE )
print p.stdout.read()
#outputs the files correctly
p.stdin.write(" ls\n")
#IOError: [Errno 32] Broken pipe
-----------

You are seeing this error because sub.Popen closes both stdin and
stdout once the subprocess terminates (which it must have done for
p.stdout.read() to return a result).

Consequently you are trying to write to a pipeline whose reader has
already closed it, hence the error message.

regards
Steve
Hi,

Thanks for the response. So are you saying that the only way you can
get data out of a pipe is when the subprocess has terminated?

Jul 2 '07 #3
Why doesn't the following program write to the file?

driver.py
-------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )
p.stdin.write(" text3")

while True:
pass
-------

test1.py:
---------
import sys

data = sys.stdin.read( )

f = open("aaa.txt", "w")
f.write(data + "\n")
f.close()
-----------
After I hit Ctrl+C to end the program and look in the file, the text
wasn't written to the file. But, if I change driver.py to the
following it works:

driver.py:
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )
p.stdin.write(" text3")
-------

Ok. So that looks like the data is caught in a buffer--even though the
pipes should be unbuffered by default. But this doesn't work:

driver.py
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )

p.stdin.write(" text4")
p.stdin.flush()

while True:
pass
-------

It just hangs, and then when I hit Ctrl+C and look in the file, the
data isn't in there.


Jul 2 '07 #4
7stud wrote:
Thanks for the response. So are you saying that the only way you
can get data out of a pipe is when the subprocess has terminated?
No, not only because Pipes aren't related to processes in any
special way.

He said that you can't write to a pipe whose reader has already
terminated.

Regards,
Björn

--
BOFH excuse #36:

dynamic software linking table corrupted

Jul 2 '07 #5
7stud wrote:
Why doesn't the following program write to the file?
[...]
It just hangs, and then when I hit Ctrl+C and look in the file,
the data isn't in there.
I suppose your running child process isn't closed cleanly if you
terminate the parent process. Also, the pipe may be unbuffered by
default; file access isn't.

Regards,
Björn

--
BOFH excuse #384:

it's an ID-10-T error

Jul 2 '07 #6
On Jul 2, 1:58 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
7stud wrote:
Thanks for the response. So are you saying that the only way you
can get data out of a pipe is when the subprocess has terminated?

No, not only because Pipes aren't related to processes in any
special way.

He said that you can't write to a pipe whose reader has already
terminated.
What he said was:
>...once the subprocess terminates (which it must have done for
p.stdout.read( ) to return a result)
And based on the results of the examples I posted in my last post, it
seems to confirm that no data travels through a pipe until a program
on one side of the pipe has terminated.

Jul 2 '07 #7
On Jul 2, 2:03 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
7stud wrote:
Why doesn't the following program write to the file?
[...]
It just hangs, and then when I hit Ctrl+C and look in the file,
the data isn't in there.

Also, the pipe may be unbuffered by
default; file access isn't.
f.close() flushes the buffer to a file.
Jul 2 '07 #8
7stud wrote:
Why doesn't the following program write to the file?

driver.py
-------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )
p.stdin.write(" text3")

while True:
pass
-------

test1.py:
---------
import sys

data = sys.stdin.read( )
Let me ask you a question: what conditions have to be true to this
statement to terminate?

A: the Python driver.py process has to close its output file. Since it
doesn't do this (instead writing a little bit of output then going into
an infinite loop) the whole thing just sits there consuming CPU time.

f = open("aaa.txt", "w")
f.write(data + "\n")
f.close()
-----------
After I hit Ctrl+C to end the program and look in the file, the text
wasn't written to the file. But, if I change driver.py to the
following it works:

driver.py:
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )
p.stdin.write(" text3")
-------

Ok. So that looks like the data is caught in a buffer--even though the
pipes should be unbuffered by default. But this doesn't work:
Who told you pipes should be unbuffered by default, and what difference
does that make anyway?

The reason it works now is that your program closes its standard output
by default when it terminates.
driver.py
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE )

p.stdin.write(" text4")
p.stdin.flush()

while True:
pass
-------

It just hangs, and then when I hit Ctrl+C and look in the file, the
data isn't in there.
Of course it does, for the reasons mentioned above. file.read() only
returns when it has consumed *all* the data from the file (which means
the write must close the file for the reader to be able to return).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Jul 2 '07 #9
On Jul 2, 2:12 pm, Steve Holden <s...@holdenweb .comwrote:
a) Who told you pipes should be unbuffered by default, and b) what difference
does that make anyway?
a) The docs.

b) If the pipes were buffered then writing a small amount of data like
"text3" to the pipe would cause the other side to hang forever thereby
providing a possible explanation for the results.
>
It just hangs, and then when I hit Ctrl+C and look in the file, the
data isn't in there.

Of course it does, for the reasons mentioned above. file.read() only
returns when it has consumed *all* the data from the file (which means
the write must close the file for the reader to be able to return).
That doesn't seem like a very good explanation, since the only thing
written to the file(i.e. stdin) was "text3", and the write() was
unbuffered, so the read() could consume all the data without the
write() closing the file--there was no more data.

Jul 2 '07 #10

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

Similar topics

0
2442
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
1
2139
by: Qiangning Hong | last post by:
I decide to seperate my data collection routine from my data analysis and storage program to a seperate process, so I try to use the new subprocess model in Python 2.4. The main program spawns the subprocess and receives data from the pipe. When some event occurs (e.g. the user clicks the 'Stop' button on GUI), the main program will send...
6
1722
by: Uri Nix | last post by:
Hi all, I've been trying to use (Python 2.4 on WinXP) the subprocess module to execute a shell command (nmake in this case), and pass its output to a higher level. Using the following snippet: p = subprocess.Popen(nmake,stderr=subprocess.PIPE,stdout=subprocess.PIPE, \ universal_newlines=True, bufsize=1)
3
5494
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead of pywin32, but my trouble exists with either option: import subprocess process = subprocess.Popen(, stderr=subprocess.STDOUT,...
5
14303
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that consistently gets an "invalid operand" IOError when write() is called on the pipe. So I switched to subprocess. It works fine when executed...
9
6469
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This hangs at this point. How should I handle these kind of commands (ping 127.0.0.1) with
12
4515
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it...
7
4904
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I try to do it in python like so: p = subprocess.Popen(,stdout=subprocess.PIPE,stderr=subprocess.PIPE) print p.communicate()
2
2985
by: Chuckk Hubbard | last post by:
If I run 'python -i subprocessclient.py' I expect to see the nice level of it go up 2, and the nice level of the subprocess go up 1. But all I see is the nice level of the client change. What am I doing wrong? subprocessserver.py: ---------------------------- #!/usr/bin/python2.5 import os
0
7647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7570
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7859
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5472
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2068
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1181
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.