From cac4ef49c15bfa4df77c533d1c0fc6235bc7455b Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Sat, 6 Apr 2024 18:39:00 +0300 Subject: [PATCH] Added maximum parallel --- ChangeLog.txt | 4 ++++ configs/slpkg.toml | 3 +++ slpkg/configs.py | 2 ++ slpkg/downloader.py | 7 ++++++- tests/test_configs.py | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 25aba3cd..428d0022 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,5 +1,9 @@ ## slpkg - ChangeLog +### 5.0.5 - 06/04/2024 +- Added: + * Added maximum parallel for downloading in the config file + ### 5.0.4 - 04/04/2024 - Updated: * Updated to ignore blacklist installed packages diff --git a/configs/slpkg.toml b/configs/slpkg.toml index 9ea695d9..e2b4a3cd 100644 --- a/configs/slpkg.toml +++ b/configs/slpkg.toml @@ -56,6 +56,9 @@ ASK_QUESTION = true # Alternatively, you can use the option '--parallel'. PARALLEL_DOWNLOADS = false +# Specifies number of concurrent download streams. Default is 5. +MAXIMUM_PARALLEL = 5 + # If progress bar is true, it does not print the commands as they # are executed. Default is false. [true/false] PROGRESS_BAR = false diff --git a/slpkg/configs.py b/slpkg/configs.py index 1b0789a4..9459e1f2 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -54,6 +54,7 @@ class Configs: ascii_characters: bool = True ask_question: bool = True parallel_downloads: bool = False + maximum_parallel: int = 5 progress_bar: bool = False progress_spinner: str = 'pixel' spinner_color: str = 'green' @@ -100,6 +101,7 @@ class Configs: ascii_characters: bool = config['ASCII_CHARACTERS'] file_list_suffix: str = config['FILE_LIST_SUFFIX'] parallel_downloads: bool = config['PARALLEL_DOWNLOADS'] + maximum_parallel: int = config['MAXIMUM_PARALLEL'] progress_bar: str = config['PROGRESS_BAR'] progress_spinner: str = config['PROGRESS_SPINNER'] spinner_color: str = config['SPINNER_COLOR'] diff --git a/slpkg/downloader.py b/slpkg/downloader.py index d4e0c7e5..21c1c2f4 100644 --- a/slpkg/downloader.py +++ b/slpkg/downloader.py @@ -4,7 +4,7 @@ import os import shutil from pathlib import Path -from multiprocessing import Process +from multiprocessing import Process, Semaphore from urllib.parse import unquote, urlparse from slpkg.configs import Configs @@ -33,6 +33,9 @@ class Downloader(Configs): 'curl': self.set_curl_downloader, 'lftp': self.set_lftp_downloader } + + self.semaphore = Semaphore(self.maximum_parallel) + self.option_for_parallel: bool = self.utils.is_option( ('-P', '--parallel'), flags) @@ -62,6 +65,7 @@ class Downloader(Configs): self.tools(url, path) def tools(self, url: str, path: Path) -> None: + self.semaphore.acquire() url_parse: str = urlparse(url).path self.filename: str = unquote(Path(url_parse).name) @@ -75,6 +79,7 @@ class Downloader(Configs): self.multi_process.process(self.downloader_command) self.check_if_downloaded(url, path) + self.semaphore.release() def copy_local_binary_file(self, url: str) -> None: try: diff --git a/tests/test_configs.py b/tests/test_configs.py index 22f3743e..c43e57fd 100644 --- a/tests/test_configs.py +++ b/tests/test_configs.py @@ -44,6 +44,7 @@ class TestConfigs(unittest.TestCase): self.assertEqual(True, self.configs.ascii_characters) self.assertEqual(True, self.configs.ask_question) self.assertEqual(False, self.configs.parallel_downloads) + self.assertEqual(5, self.configs.maximum_parallel) self.assertEqual(True, self.configs.progress_bar) self.assertEqual('pixel', self.configs.progress_spinner) self.assertEqual('green', self.configs.spinner_color)