IOError: [Errno 32] Broken pipe

Donn Cave donn at u.washington.edu
Mon Jun 28 20:13:12 EDT 2004


In article <Pine.LNX.4.44.0406281844290.30693-100000 at ccc2.wpi.edu>,
 Christopher T King <squirrel at WPI.EDU> wrote:

> On 28 Jun 2004, Jay Donnell wrote:
> 
> > 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, donn at u.washington.edu



More information about the Python-list mailing list