code: def _safe_exit(start, output):
      
      try:
          sys.stdout.write(output)
          sys.stdout.flush()
      except TypeError:  # python3
          sys.stdout.write(str(output, 'utf-8'))
          sys.stdout.flush()
      except IOError:
          pass
  
      seconds = time.time() - start
      print("\n\n%5.3f seconds" % (seconds), file=sys.stderr)
docs: exit without breaking pipes