Skip to content

Commit

Permalink
sync changes with pypdl
Browse files Browse the repository at this point in the history
  • Loading branch information
mjishnu committed Nov 18, 2023
1 parent 3e415bc commit e30186b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def download_install_thread(data, progress_current, progress_main):
if not os.path.exists(dwnpath):
os.makedirs(dwnpath)
path_lst = {}
d = Downloader(self.stop)
d = Downloader()
for f_name in final_data:
# Define the remote file to retrieve
remote_url = main_dict[f_name] # {f_name:url}
Expand Down Expand Up @@ -275,6 +275,7 @@ def new_url_gen():
progress_current.emit(download_percentage)
time.sleep(0.1)
if self.stop.is_set(): # check if the stop event is triggered
d.stop()
raise Exception("Stoped By User!")
if d.Failed:
raise Exception("Download Error Occured!")
Expand Down
33 changes: 22 additions & 11 deletions app/modules/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, dic, id, stop, error):
self.id = id # ID of this download part
# dictionary containing download information for all parts, {start, curr, end, filepath, count, size, url, completed}
self.dic = dic
self.stop = stop # event to stop the download
self._stop = stop # event to stop the download
self.error = error # event to indicate an error occurred

def getval(self, key):
Expand Down Expand Up @@ -60,7 +60,7 @@ def worker(self):
f.write(chunk)
self.curr += len(chunk)
self.setval("curr", self.curr)
if not chunk or self.stop.is_set() or self.error.is_set():
if not chunk or self._stop.is_set() or self.error.is_set():
break
except Exception as e:
self.error.set()
Expand Down Expand Up @@ -103,13 +103,14 @@ def worker(self, url, path, stop, error):


class Downloader:
def __init__(self, StopEvent=threading.Event()):
def __init__(self):
self._dic = {}
self._workers = []
self._threads = []
self._Error = threading.Event()
self._stop = threading.Event() # stop Event

# attributes
self.Stop = StopEvent # stop Event
self.Failed = False
self.totalMB = 0
self.progress = 0
Expand All @@ -132,7 +133,7 @@ def download(self, url, filepath, num_connections, display, multithread):
sd = Singledown()
# create single download worker thread
th = threading.Thread(
target=sd.worker, args=(url, f_path, self.Stop, self._Error)
target=sd.worker, args=(url, f_path, self._stop, self._Error)
)
self._workers.append(sd)
th.start()
Expand Down Expand Up @@ -179,12 +180,13 @@ def download(self, url, filepath, num_connections, display, multithread):
"completed": False,
}
# create multidownload object for each connection
md = Multidown(self._dic, i, self.Stop, self._Error)
md = Multidown(self._dic, i, self._stop, self._Error)
# create worker thread for each connection
th = threading.Thread(target=md.worker)
threads.append(th)
th.start()
self._workers.append(md)
self._threads.append(th)

# save the progress to the progress file
if not singlethread:
Expand All @@ -207,7 +209,7 @@ def download(self, url, filepath, num_connections, display, multithread):
self.progress = 0

# check if download has been stopped or if an error has occurred
if self.Stop.is_set() or self._Error.is_set():
if self._stop.is_set() or self._Error.is_set():
self._dic["paused"] = True
if not singlethread:
# save progress to progress file
Expand Down Expand Up @@ -237,9 +239,19 @@ def download(self, url, filepath, num_connections, display, multithread):
break
time.sleep(interval)

if display and self.Stop.is_set():
if display and self._stop.is_set():
print("Task interrupted")

def stop(self):
"""
Stop the download process.
"""
time.sleep(2)
self._stop.set()
# waiting for all threads to be killed by the poison pill
for thread in self._threads:
thread.join()

def start(
self,
url,
Expand Down Expand Up @@ -274,7 +286,7 @@ def start_thread():
if self._Error.is_set():
time.sleep(3)
# reset the downloader object
self.__init__(self.Stop)
self.__init__()

# get a new download URL to retry
_url = url
Expand Down Expand Up @@ -305,8 +317,7 @@ def start_thread():
print("Download Failed!")

# Initialize the downloader with stop Event
self.__init__(self.Stop)
self.Stop.clear()
self.__init__()
# Start the download process in a new thread
th = threading.Thread(target=start_thread)
th.start()
Expand Down

0 comments on commit e30186b

Please sign in to comment.