Extend Qthread.terminate for DownloadThread class

This commit is contained in:
alessandro90
2019-06-08 21:27:09 +02:00
parent 72936a4747
commit c0e637886b

View File

@@ -45,6 +45,7 @@ class DownloadThread(BaseDownloadThread):
def __init__(self): def __init__(self):
"""Just call super().__init__.""" """Just call super().__init__."""
self.db = None
super().__init__() super().__init__()
def _pretty_len(self, byte_obj): def _pretty_len(self, byte_obj):
@@ -67,16 +68,17 @@ class DownloadThread(BaseDownloadThread):
Handle all possible exceptions. Also extract the files Handle all possible exceptions. Also extract the files
in the local folder.""" in the local folder."""
self.status = ThreadStatus.UNDEFINED self.status = ThreadStatus.UNDEFINED
self.db = None
raw_data = bytes(0) raw_data = bytes(0)
try: try:
db = urllib3.PoolManager().request( self.db = urllib3.PoolManager().request(
'GET', 'GET',
Database.LINK_LOC, Database.LINK_LOC,
preload_content=False preload_content=False
) )
while True: while True:
start = time() start = time()
data = db.read(self.CHUNK) data = self.db.read(self.CHUNK)
delta = time() - start delta = time() - start
if not data: if not data:
break break
@@ -85,12 +87,12 @@ class DownloadThread(BaseDownloadThread):
self._pretty_len(raw_data), self._pretty_len(raw_data),
self._get_download_speed(data, delta) self._get_download_speed(data, delta)
) )
db.release_conn() self.db.release_conn()
except Exception: # No internet connection. except Exception: # No internet connection.
db.release_conn() self.db.release_conn()
self.status = ThreadStatus.NO_CONNECTION_ERR self.status = ThreadStatus.NO_CONNECTION_ERR
return return
if db.status != 200: if self.db.status != 200:
self.status = ThreadStatus.BAD_DOWNLOAD_ERR self.status = ThreadStatus.BAD_DOWNLOAD_ERR
return return
try: try:
@@ -113,6 +115,14 @@ class DownloadThread(BaseDownloadThread):
else: else:
self.status = ThreadStatus.OK self.status = ThreadStatus.OK
def terminate(self):
"""Extend QThread.terminate.
Release the connection in case of termination."""
if self.db is not None:
self.db.release_conn()
super().terminate()
class _AsyncDownloader: class _AsyncDownloader:
"""Mixin class for asynchronous threads.""" """Mixin class for asynchronous threads."""