IOError: [Errno 32] Broken pipe

J

Jay Donnell

I'm working on a simple script to manipulate csv files. Right now it
just prints the first field of the file for each line. Everything
works fine, but if I use 'head' or 'more' and quit while in more then
I get
IOError: [Errno 32] Broken pipe

Anyone know why this is happening?

jay@linux:~/Desktop/gmProductData> ./variance.py | head
91109 : 91109
A0101 : A0101
A0102 : A0102
A0103 : A0103
A0104 : A0104
A0105 : A0105
A0106 : A0106
A0107 : A0107
A0111 : A0111
A0112 : A0112
Traceback (most recent call last):
File "./variance.py", line 39, in ?
print '%s : %s' % (match, skuS[0])
IOError: [Errno 32] Broken pipe
 
C

Christopher T King

I'm working on a simple script to manipulate csv files. Right now it
just prints the first field of the file for each line. Everything
works fine, but if I use 'head' or 'more' and quit while in more then
I get
IOError: [Errno 32] Broken pipe

Anyone know why this is happening?

That's normal, at least with Unix. When the program on the receiving end
of a pipe decides to close its end for some reason, Unix sends the signal
'SIGPIPE' to the sending end. Python catches this and turns it into an
IOError exception. The only way around this (that I can think of) is to
catch the exception and exit the program gracefully. If you try to send
more data, you will get more IOErrors, since your program has nowhere left
to send data.
 
D

Donn Cave

Christopher T King said:
I'm working on a simple script to manipulate csv files. Right now it
just prints the first field of the file for each line. Everything
works fine, but if I use 'head' or 'more' and quit while in more then
I get
IOError: [Errno 32] Broken pipe

Anyone know why this is happening?

That's normal, at least with Unix. When the program on the receiving end
of a pipe decides to close its end for some reason, Unix sends the signal
'SIGPIPE' to the sending end. Python catches this and turns it into an
IOError exception. The only way around this (that I can think of) is to
catch the exception and exit the program gracefully. If you try to send
more data, you will get more IOErrors, since your program has nowhere left
to send data.

Actually the problem is not that Python catches SIGPIPE, but
rather that it ignores it - as in, signal(SIGPIPE, SIG_IGN)
Then the write returns an error 32 EPIPE, which naturally
turns into an exception.

To restore normal UNIX behavior,

import signal

signal.signal(signal.SIGPIPE, signal.SIG_DFL)

And also do that after any instantiation of a socket object,
because it happens there too.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top