From 049147829cb4e95e0c2244c7b1c31858abaae8c0 Mon Sep 17 00:00:00 2001 From: Max Hawkins Date: Sun, 13 Sep 2015 16:24:08 +0700 Subject: [PATCH] Handle EPIPE error in sample.py Previously, the program would raise an exception if piped to a program that closes the pipe before all the data is written. For example: cat data.txt | sample.py 1/10 | head -n 1 --- data_hacks/sample.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data_hacks/sample.py b/data_hacks/sample.py index c3296ab..5d42548 100755 --- a/data_hacks/sample.py +++ b/data_hacks/sample.py @@ -20,6 +20,7 @@ https://github.com/bitly/data_hacks """ +import errno import sys import random from optparse import OptionParser @@ -29,7 +30,13 @@ def run(sample_rate): input_stream = sys.stdin for line in input_stream: if random.randint(1,100) <= sample_rate: - sys.stdout.write(line) + try: + sys.stdout.write(line) + except IOError, e: + if e.errno == errno.EPIPE: + return + else: + raise def get_sample_rate(rate_string): """ return a rate as a percentage"""