Threading and callback improvements
This commit is contained in:
parent
537c5aeda0
commit
aa52e550bf
40
speedtest.py
40
speedtest.py
|
@ -368,30 +368,36 @@ def print_dots(current, total, start=False, end=False):
|
||||||
status
|
status
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if SHUTDOWN_EVENT.isSet():
|
||||||
|
return
|
||||||
|
|
||||||
sys.stdout.write('.')
|
sys.stdout.write('.')
|
||||||
if current + 1 == total and end is True:
|
if current + 1 == total and end is True:
|
||||||
sys.stdout.write('\n')
|
sys.stdout.write('\n')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def do_nothing(*args, **kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HTTPDownloader(threading.Thread):
|
class HTTPDownloader(threading.Thread):
|
||||||
"""Thread class for retrieving a URL"""
|
"""Thread class for retrieving a URL"""
|
||||||
|
|
||||||
def __init__(self, i, url, start, timeout):
|
def __init__(self, i, url, start, timeout):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
self.url = url
|
self.url = url
|
||||||
self.result = None
|
self.result = [0]
|
||||||
self.starttime = start
|
self.starttime = start
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.i = i
|
self.i = i
|
||||||
threading.Thread.__init__(self)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.result = [0]
|
|
||||||
try:
|
try:
|
||||||
if (timeit.default_timer() - self.starttime) <= self.timeout:
|
if (timeit.default_timer() - self.starttime) <= self.timeout:
|
||||||
request = build_request(self.url)
|
request = build_request(self.url)
|
||||||
f = urlopen(request)
|
f = urlopen(request)
|
||||||
while (1 and not SHUTDOWN_EVENT.isSet() and
|
while (not SHUTDOWN_EVENT.isSet() and
|
||||||
(timeit.default_timer() - self.starttime) <=
|
(timeit.default_timer() - self.starttime) <=
|
||||||
self.timeout):
|
self.timeout):
|
||||||
self.result.append(len(f.read(10240)))
|
self.result.append(len(f.read(10240)))
|
||||||
|
@ -436,6 +442,7 @@ class HTTPUploader(threading.Thread):
|
||||||
"""Thread class for putting a URL"""
|
"""Thread class for putting a URL"""
|
||||||
|
|
||||||
def __init__(self, i, url, start, size, timeout):
|
def __init__(self, i, url, start, size, timeout):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
self.url = url
|
self.url = url
|
||||||
self.data = HTTPUploaderData(size, start, timeout)
|
self.data = HTTPUploaderData(size, start, timeout)
|
||||||
self.size = size
|
self.size = size
|
||||||
|
@ -443,7 +450,6 @@ class HTTPUploader(threading.Thread):
|
||||||
self.starttime = start
|
self.starttime = start
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.i = i
|
self.i = i
|
||||||
threading.Thread.__init__(self)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
|
@ -951,7 +957,7 @@ class Speedtest(object):
|
||||||
printer(best, debug=True)
|
printer(best, debug=True)
|
||||||
return best
|
return best
|
||||||
|
|
||||||
def download(self, callback=None):
|
def download(self, callback=do_nothing):
|
||||||
"""Test download speed against speedtest.net"""
|
"""Test download speed against speedtest.net"""
|
||||||
|
|
||||||
urls = []
|
urls = []
|
||||||
|
@ -970,8 +976,7 @@ class Speedtest(object):
|
||||||
self.config['length']['download'])
|
self.config['length']['download'])
|
||||||
thread.start()
|
thread.start()
|
||||||
q.put(thread, True)
|
q.put(thread, True)
|
||||||
if not SHUTDOWN_EVENT.isSet() and callback:
|
callback(i, url_count, start=True)
|
||||||
callback(i, url_count, start=True)
|
|
||||||
|
|
||||||
finished = []
|
finished = []
|
||||||
|
|
||||||
|
@ -981,8 +986,7 @@ class Speedtest(object):
|
||||||
while thread.isAlive():
|
while thread.isAlive():
|
||||||
thread.join(timeout=0.1)
|
thread.join(timeout=0.1)
|
||||||
finished.append(sum(thread.result))
|
finished.append(sum(thread.result))
|
||||||
if not SHUTDOWN_EVENT.isSet() and callback:
|
callback(thread.i, url_count, end=True)
|
||||||
callback(thread.i, url_count, end=True)
|
|
||||||
del thread
|
del thread
|
||||||
|
|
||||||
q = Queue(self.config['threads']['download'])
|
q = Queue(self.config['threads']['download'])
|
||||||
|
@ -1004,7 +1008,7 @@ class Speedtest(object):
|
||||||
self.config['threads']['upload'] = 8
|
self.config['threads']['upload'] = 8
|
||||||
return self.results.download
|
return self.results.download
|
||||||
|
|
||||||
def upload(self, callback=None):
|
def upload(self, callback=do_nothing):
|
||||||
"""Test upload speed against speedtest.net"""
|
"""Test upload speed against speedtest.net"""
|
||||||
|
|
||||||
sizes = []
|
sizes = []
|
||||||
|
@ -1025,8 +1029,7 @@ class Speedtest(object):
|
||||||
self.config['length']['upload'])
|
self.config['length']['upload'])
|
||||||
thread.start()
|
thread.start()
|
||||||
q.put(thread, True)
|
q.put(thread, True)
|
||||||
if not SHUTDOWN_EVENT.isSet() and callback:
|
callback(i, size_count, start=True)
|
||||||
callback(i, size_count, start=True)
|
|
||||||
|
|
||||||
finished = []
|
finished = []
|
||||||
|
|
||||||
|
@ -1036,8 +1039,7 @@ class Speedtest(object):
|
||||||
while thread.isAlive():
|
while thread.isAlive():
|
||||||
thread.join(timeout=0.1)
|
thread.join(timeout=0.1)
|
||||||
finished.append(thread.result)
|
finished.append(thread.result)
|
||||||
if not SHUTDOWN_EVENT.isSet() and callback:
|
callback(thread.i, size_count, end=True)
|
||||||
callback(thread.i, size_count, end=True)
|
|
||||||
del thread
|
del thread
|
||||||
|
|
||||||
q = Queue(self.config['threads']['upload'])
|
q = Queue(self.config['threads']['upload'])
|
||||||
|
@ -1211,7 +1213,7 @@ def shell():
|
||||||
|
|
||||||
# Don't set a callback if we are running quietly
|
# Don't set a callback if we are running quietly
|
||||||
if quiet or debug:
|
if quiet or debug:
|
||||||
callback = None
|
callback = do_nothing
|
||||||
else:
|
else:
|
||||||
callback = print_dots
|
callback = print_dots
|
||||||
|
|
||||||
|
@ -1275,13 +1277,15 @@ def shell():
|
||||||
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
printer('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % results.server, quiet)
|
'%(latency)s ms' % results.server, quiet)
|
||||||
|
|
||||||
printer('Testing download speed', quiet, end=('\n', '')[bool(callback)])
|
printer('Testing download speed', quiet,
|
||||||
|
end=('', '\n')[bool(debug)])
|
||||||
speedtest.download(callback=callback)
|
speedtest.download(callback=callback)
|
||||||
printer('Download: %0.2f M%s/s' %
|
printer('Download: %0.2f M%s/s' %
|
||||||
((results.download / 1000 / 1000), args.units[0]),
|
((results.download / 1000 / 1000), args.units[0]),
|
||||||
quiet)
|
quiet)
|
||||||
|
|
||||||
printer('Testing upload speed', quiet, end=('\n', '')[bool(callback)])
|
printer('Testing upload speed', quiet,
|
||||||
|
end=('', '\n')[bool(debug)])
|
||||||
speedtest.upload(callback=callback)
|
speedtest.upload(callback=callback)
|
||||||
printer('Upload: %0.2f M%s/s' %
|
printer('Upload: %0.2f M%s/s' %
|
||||||
((results.upload / 1000 / 1000), args.units[0]),
|
((results.upload / 1000 / 1000), args.units[0]),
|
||||||
|
|
Loading…
Reference in New Issue