Created
February 13, 2015 12:30
-
-
Save greenarrow/3fa735ba16bae6311c36 to your computer and use it in GitHub Desktop.
How to make Python play nicely with pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import errno | |
def main(): | |
for i in range(20): | |
print i | |
if __name__ == "__main__": | |
# ensure buffer is flushed before we handle SIGPIPE | |
try: | |
try: | |
main() | |
finally: | |
sys.stdout.flush() | |
except IOError, e: | |
if e.errno != errno.EPIPE: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment