title: Python - How to catch a broken pipe
body: <p>I have just learned about SIGPIPE, and then read about how to handle these in Python. </p>
  
  <p>Among other sources, I have read: <a href="https://stackoverflow.com/questions/180095/how-to-handle-a-broken-pipe-sigpipe-in-python">How to handle a broken pipe (SIGPIPE) in python?</a></p>
  
  <p>Let's say that the pipe reading script exits, then all the answers suggest that the writing script wrappes its write calls in a try clause. </p>
  
  <p>However, I can not make this work. This is my code:</p>
  
  <pre><code># printer.py
  import time
  try:
      for i in range(10):
          time.sleep(1)
          print i
  except:
      print 'We caught the problem'
  </code></pre>
  
  <p>and</p>
  
  <pre><code>#nonreader.py
  #read nothing, but stay alive for 5 sec
  import time, sys
  time.sleep(5)
  sys.exit(0)
  </code></pre>
  
  <p>And in the shell:</p>
  
  <pre><code>$ python printer.py | python nonreader.py 
  close failed in file object destructor:
  Error in sys.excepthook:
  
  Original exception was:
  </code></pre>
  
  <p>Obviously, nothing was caught. And furthermore, it looks really wrong, when it prints 'Original exception was:' and then no more. </p>
  
  <p>What is wrong / what have I misunderstood?</p>
  
  <p>Thomas</p>