From 6fc01d225e4cc3abca8c32c927c5049bb2a9e165 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Mon, 22 Jul 2024 23:08:50 +0300 Subject: [PATCH] Fixed for max parallel download Signed-off-by: Dimitris Zlatanidis --- ChangeLog.txt | 1 + slpkg/downloader.py | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d8884ba6..96d5924c 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -8,6 +8,7 @@ - Fixed: * Fixed to create custom repositories (Thanks to mac-a-r0ni) + * Fixed OSError: [Errno 24] too many open files for parallel download (Thanks to rizitis) ### 5.1.0 - 06/06/2024 diff --git a/slpkg/downloader.py b/slpkg/downloader.py index 081942df..907c7358 100644 --- a/slpkg/downloader.py +++ b/slpkg/downloader.py @@ -3,8 +3,9 @@ import os +import threading from pathlib import Path -from multiprocessing import Process, Semaphore +from multiprocessing import Process from urllib.parse import unquote, urlparse from slpkg.configs import Configs @@ -35,7 +36,8 @@ class Downloader(Configs): # pylint: disable=[R0902] 'lftp': self.set_lftp_downloader } - self.semaphore = Semaphore(self.maximum_parallel) + # Semaphore to control the number of concurrent threads + self.semaphore = threading.BoundedSemaphore(int(self.maximum_parallel)) self.option_for_parallel: bool = self.utils.is_option( ('-P', '--parallel'), flags) @@ -51,10 +53,11 @@ class Downloader(Configs): # pylint: disable=[R0902] """Download sources with parallel mode.""" processes: list = [] for urls, path in sources.values(): - for url in urls: - proc = Process(target=self.tools, args=(url, path)) - processes.append(proc) - proc.start() + with self.semaphore: + for url in urls: + proc = Process(target=self.tools, args=(url, path)) + processes.append(proc) + proc.start() for process in processes: process.join() @@ -72,7 +75,6 @@ class Downloader(Configs): # pylint: disable=[R0902] url (str): The URL link. path (Path): Path to save. """ - self.semaphore.acquire() url_parse: str = urlparse(url).path self.filename: str = unquote(Path(url_parse).name) @@ -83,7 +85,6 @@ class Downloader(Configs): # pylint: disable=[R0902] self.multi_process.process(self.downloader_command) self.check_if_downloaded(url, path) - self.semaphore.release() def set_wget_downloader(self, url: str, path: Path) -> None: """Set for wget tool.