From bf3609700e5680085a2dca02c99dd678f80597e2 Mon Sep 17 00:00:00 2001 From: Romuald Brunet Date: Tue, 13 Sep 2016 10:06:00 +0200 Subject: [PATCH] Ignore broken pipes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ignore broken pipes to allow pipping to other programs (less, head, …) without python raising an exception when output is cut short --- speedtest_cli.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/speedtest_cli.py b/speedtest_cli.py index d93d5c3..7cdc0f1 100755 --- a/speedtest_cli.py +++ b/speedtest_cli.py @@ -541,6 +541,16 @@ def ctrl_c(signum, frame): raise SystemExit('\nCancelling...') +def sig_pipe(signum, frame): + """Ignore the PIPE signal when trying to output on a closed file handle, + and simply exit + + """ + global shutdown_event + shutdown_event.set() + raise SystemExit + + def version(): """Print the version""" @@ -554,6 +564,7 @@ def speedtest(): shutdown_event = threading.Event() signal.signal(signal.SIGINT, ctrl_c) + signal.signal(signal.SIGPIPE, sig_pipe) description = ( 'Command line interface for testing internet bandwidth using '