Merge branch 'sigint', Fixes #33

This commit is contained in:
Matt Martz 2013-09-06 14:59:21 -05:00
commit 498f0004f1
1 changed files with 31 additions and 18 deletions

View File

@ -26,6 +26,7 @@ import os
import sys import sys
import threading import threading
import re import re
import signal
from xml.dom import minidom as DOM from xml.dom import minidom as DOM
try: try:
@ -138,15 +139,12 @@ class FileGetter(threading.Thread):
self.starttime = start self.starttime = start
threading.Thread.__init__(self) threading.Thread.__init__(self)
def get_result(self):
return self.result
def run(self): def run(self):
self.result = [0] self.result = [0]
try: try:
if (time.time() - self.starttime) <= 10: if (time.time() - self.starttime) <= 10:
f = urlopen(self.url) f = urlopen(self.url)
while 1: while 1 and not shutdown_event.is_set():
self.result.append(len(f.read(10240))) self.result.append(len(f.read(10240)))
if self.result[-1] == 0: if self.result[-1] == 0:
break break
@ -163,7 +161,7 @@ def downloadSpeed(files, quiet=False):
thread = FileGetter(file, start) thread = FileGetter(file, start)
thread.start() thread.start()
q.put(thread, True) q.put(thread, True)
if not quiet: if not quiet and not shutdown_event.is_set():
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
@ -172,18 +170,21 @@ def downloadSpeed(files, quiet=False):
def consumer(q, total_files): def consumer(q, total_files):
while len(finished) < total_files: while len(finished) < total_files:
thread = q.get(True) thread = q.get(True)
thread.join() while thread.is_alive():
thread.join(timeout=0.1)
finished.append(sum(thread.result)) finished.append(sum(thread.result))
del thread del thread
q = Queue(6) q = Queue(6)
start = time.time()
prod_thread = threading.Thread(target=producer, args=(q, files)) prod_thread = threading.Thread(target=producer, args=(q, files))
cons_thread = threading.Thread(target=consumer, args=(q, len(files))) cons_thread = threading.Thread(target=consumer, args=(q, len(files)))
start = time.time()
prod_thread.start() prod_thread.start()
cons_thread.start() cons_thread.start()
prod_thread.join() while prod_thread.is_alive():
cons_thread.join() prod_thread.join(timeout=0.1)
while cons_thread.is_alive():
cons_thread.join(timeout=0.1)
return (sum(finished)/(time.time()-start)) return (sum(finished)/(time.time()-start))
@ -198,12 +199,10 @@ class FilePutter(threading.Thread):
self.starttime = start self.starttime = start
threading.Thread.__init__(self) threading.Thread.__init__(self)
def get_result(self):
return self.result
def run(self): def run(self):
try: try:
if (time.time() - self.starttime) <= 10: if ((time.time() - self.starttime) <= 10 and
not shutdown_event.is_set()):
f = urlopen(self.url, self.data) f = urlopen(self.url, self.data)
f.read(11) f.read(11)
f.close() f.close()
@ -222,7 +221,7 @@ def uploadSpeed(url, sizes, quiet=False):
thread = FilePutter(url, start, size) thread = FilePutter(url, start, size)
thread.start() thread.start()
q.put(thread, True) q.put(thread, True)
if not quiet: if not quiet and not shutdown_event.is_set():
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
@ -231,18 +230,21 @@ def uploadSpeed(url, sizes, quiet=False):
def consumer(q, total_sizes): def consumer(q, total_sizes):
while len(finished) < total_sizes: while len(finished) < total_sizes:
thread = q.get(True) thread = q.get(True)
thread.join() while thread.is_alive():
thread.join(timeout=0.1)
finished.append(thread.result) finished.append(thread.result)
del thread del thread
q = Queue(6) q = Queue(6)
start = time.time()
prod_thread = threading.Thread(target=producer, args=(q, sizes)) prod_thread = threading.Thread(target=producer, args=(q, sizes))
cons_thread = threading.Thread(target=consumer, args=(q, len(sizes))) cons_thread = threading.Thread(target=consumer, args=(q, len(sizes)))
start = time.time()
prod_thread.start() prod_thread.start()
cons_thread.start() cons_thread.start()
prod_thread.join() while prod_thread.is_alive():
cons_thread.join() prod_thread.join(timeout=0.1)
while cons_thread.is_alive():
cons_thread.join(timeout=0.1)
return (sum(finished)/(time.time()-start)) return (sum(finished)/(time.time()-start))
@ -338,9 +340,20 @@ def getBestServer(servers):
return best return best
def ctrl_c(signum, frame):
global shutdown_event
shutdown_event.set()
raise SystemExit('\nCancelling...')
def speedtest(): def speedtest():
"""Run the full speedtest.net test""" """Run the full speedtest.net test"""
global shutdown_event
shutdown_event = threading.Event()
signal.signal(signal.SIGINT, ctrl_c)
description = ( description = (
'Command line interface for testing internet bandwidth using ' 'Command line interface for testing internet bandwidth using '
'speedtest.net.\n' 'speedtest.net.\n'