473,382 Members | 1,377 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,382 software developers and data experts.

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 21131
On Jul 2, 1:12 pm, 7stud <bbxx789_0...@yahoo.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...@gmail.com" <holden...@gmail.comwrote:
On Jul 2, 1:12 pm, 7stud <bbxx789_0...@yahoo.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.comwrote:
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.comwrote:
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
7stud wrote:
On Jul 2, 1:58 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
>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.
No, you plonker!

No data is produced *by .read()* until the writer has closed it.

I really don't remember anyone in recent history as eager to willfully
misunderstand any attempted assistance. Please try to read what is
written more carefully. It's most annoying when "the better the advice
the worse it's wasted", as the Scots say.

Please forgive my brusqueness.

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 #11
7stud wrote:
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.
[sigh].

So please explain how the receiving process mysteriously manages to look
inside your producer process to know that it is never going to produce
any more data. Let's (briefly) look at the docs for read():

"""
read( [size])

Read at most size bytes from the file (less if the read hits EOF before
obtaining size bytes). If the size argument is negative or omitted, read
all data until EOF is reached. ...
"""

I believe you omitted the argument. As I have explained, the read() call
therefore waits until the writer has closed the file. Which is what
makes the EOF indication appear.

And please stop dragging buffering into this as a red herring. You do
know what buffering *is*, I take it? The read() call buffers even an
unbuffered source, by definition.

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 #12

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

Similar topics

0
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
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...
6
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...
3
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...
5
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...
9
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...
12
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...
7
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.