Jay Taylor | programmer notes



Python Error/Exception: IOError: [Errno 32] Broken pipe

So, I got this weird exception when I ran the command:

python script_that_prints_output.py | head
..[some output, in fact, just the amount that head wanted(!)]..
Traceback (most recent call last):
File "./script_that_prints_output.py", line 86, in
print out
IOError: [Errno 32] Broken pipe

I was not sure why this was happening, because when I ran the command without piping it to “head”, it ran just fine, displaying the expected output with no exceptions or errors raised.

I found the explanation for this behavior, and thought it was interesting enough to warrant a post (that way if I encounter it again at some point in the future, the odds that I’ll remember are probably greater ;):

> 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.

I actually thought of anohter way around it, albeit not efficient in any way (the whole point of the SIGPIPE signal is to stop the program from completing, since no further output is required from it).  This is definitely a rather ugly hack, but sometimes that is preferable to adding the exception handling, I guess.

python script_that_prints_output.py > tmpfile ; head tmpfile; rm tmpfile

Update 2009-12-17:  Useful information about broken pipes in Python at StackOverflow

RSS Feed

No comments yet.

Leave a comment!

You must be logged in to post a comment.



Find it!