From 9c0b93cb321f8f6ad57444900413185a6e07d3aa Mon Sep 17 00:00:00 2001 From: alessandro90 Date: Fri, 28 Jun 2019 20:17:00 +0200 Subject: [PATCH] Add a 'Slow connection' warning. Use perf_counter() in place of time() and also lower the _CHUNK in the database download thread --- constants.py | 2 ++ download_window.py | 13 ++++++++++++- threads.py | 24 ++++++++++++++++-------- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/constants.py b/constants.py index 0057942..59b0656 100644 --- a/constants.py +++ b/constants.py @@ -52,6 +52,8 @@ class Messages: NO_CONNECTION_MSG = "Unable to establish an internet connection." BAD_DOWNLOAD = "Something went wrong" BAD_DOWNLOAD_MSG = "Something went wrong with the downaload.\nCheck your internet connection and try again." + SLOW_CONN = "Slow internet connection" + SLOW_CONN_MSG = "Your internet connection is unstable or too slow." class Signal: diff --git a/download_window.py b/download_window.py index 7d16728..763cdc8 100644 --- a/download_window.py +++ b/download_window.py @@ -37,6 +37,10 @@ class DownloadWindow(QWidget, Ui_Download_window): text=Messages.BAD_DOWNLOAD_MSG, connection=self.close) + self._slow_conn_msg = pop_up(self, title=Messages.SLOW_CONN, + text=Messages.SLOW_CONN_MSG, + connection=self.close) + self._download_thread = DownloadThread() self._download_thread.finished.connect(self._wait_close) self._download_thread.progress.connect(self._display_progress) @@ -49,7 +53,12 @@ class DownloadWindow(QWidget, Ui_Download_window): def _downlaod_format_str(self, n, speed): """Return a well-formatted string with downloaded MB and speed.""" - return f"Downloaded: {n} MB\nSpeed: {speed} MB/s" + ret = f"Downloaded: {n} MB\nSpeed: " + if speed == 0.0: + ret += "VERY SLOW" + else: + ret += f"{speed} MB/s" + return ret def show(self): """Extends QWidget.show. Set downloaded MB and speed to zero.""" @@ -86,6 +95,8 @@ class DownloadWindow(QWidget, Ui_Download_window): self._no_internet_msg.show() elif self._download_thread.status is ThreadStatus.BAD_DOWNLOAD_ERR: self._bad_db_download_msg.show() + elif self._download_thread.status is ThreadStatus.SLOW_CONN_ERR: + self._slow_conn_msg.show() else: self.close() diff --git a/threads.py b/threads.py index fc0a122..60569ee 100644 --- a/threads.py +++ b/threads.py @@ -4,7 +4,7 @@ from io import BytesIO from math import ceil import os.path from shutil import rmtree -from time import time +from time import perf_counter from zipfile import ZipFile import aiohttp import urllib3 @@ -21,6 +21,11 @@ class ThreadStatus(Enum): UNKNOWN_ERR = auto() BAD_DOWNLOAD_ERR = auto() UNDEFINED = auto() + SLOW_CONN_ERR = auto() + + +class SlowConnError(Exception): + pass class BaseDownloadThread(QThread): @@ -41,7 +46,7 @@ class DownloadThread(BaseDownloadThread): """Subclass BaseDownloadThread. Download the database, images and audio samples.""" progress = pyqtSignal(int, float) - _CHUNK = 512 * 1024 + _CHUNK = 128 * 1024 _MEGA = 1024**2 def __init__(self): @@ -83,13 +88,13 @@ class DownloadThread(BaseDownloadThread): timeout=4.0 ) while True: - start = time() + start = perf_counter() try: data = self._db.read(self._CHUNK) except Exception: - raise + raise SlowConnError else: - delta = time() - start + delta = perf_counter() - start if not data: break raw_data += data @@ -101,16 +106,19 @@ class DownloadThread(BaseDownloadThread): self._exit_call = False self._db.release_conn() return - except Exception: # No internet connection. + except Exception as e: # No (or bad) internet connection. self._db.release_conn() - self.status = ThreadStatus.NO_CONNECTION_ERR + if isinstance(e, SlowConnError): + self.status = ThreadStatus.SLOW_CONN_ERR + else: + self.status = ThreadStatus.NO_CONNECTION_ERR return if self._db.status != 200: self.status = ThreadStatus.BAD_DOWNLOAD_ERR return try: is_checksum_ok = checksum_ok(raw_data, ChecksumWhat.FOLDER) - except Exception: + except Exception: # checksum_ok unable to connect to the reference. self.status = ThreadStatus.NO_CONNECTION_ERR return else: