From e9fa0aac2167551490dd6e713dc22f4f48714ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantin=20F=C3=BCrst?= Date: Mon, 29 Aug 2022 23:29:50 +0200 Subject: [PATCH] enable multithreading support by building a command list and iterating over it in parallel with thread amount configurable in ini --- libconv.py | 20 +++++++++++++++++++- quality-flac.ini | 1 + small-mp3.ini | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libconv.py b/libconv.py index 23fcdc9..facb6dc 100644 --- a/libconv.py +++ b/libconv.py @@ -7,6 +7,7 @@ import subprocess import sys import re +from joblib import Parallel, delayed from configparser import ConfigParser from copy import deepcopy from pathlib import Path @@ -27,12 +28,15 @@ DRY_RUN: bool = config.get("libconv", "dry_run") != "false" DEDUCE_METADATA: bool = config.get("libconv", "deduce_metadata") == "true" FFMPEG_LOCATION: str = config.get("ffmpeg", "location") FFMPEG_OPTS: str = config.get("ffmpeg", "options") +THREADS: int = int(config.get("libconv", "threads")) if DST_OVERWRITE: FFMPEG_OPTS += " -y" logging.basicConfig(level=logging.INFO) +cmd_list: List[str] = [] + # ----------------------------------------------------------------- # # file and folder operations @@ -49,7 +53,7 @@ def mkdir(path: str): def execute(command: str): logging.debug(f"execute: {command}") if not DRY_RUN: - os.system(command) + cmd_list.append(command) # ----------------------------------------------------------------- # # cue sheet processing @@ -210,6 +214,18 @@ def process_folder(path: str): logging.info("scanning subfolders") scan_folder(path) +def execute_command_list(): + logging.info(f"Executing all {len(cmd_list)} commands with {THREADS} parallel threads") + + if DRY_RUN: + logging.info("Skipping execution as we are dry-running. Printing list as debug-info.") + logging.debug(str(cmd_list)) + return + + Parallel(n_jobs=THREADS)( + delayed(os.system)(cmd) for cmd in cmd_list + ) + if __name__ == "__main__": logging.info(f"Using settings from {CONFIG_FILE}") @@ -219,3 +235,5 @@ if __name__ == "__main__": logging.info("Overwrite of existing files active") process_folder(SRC_FOLDER) + execute_command_list() + diff --git a/quality-flac.ini b/quality-flac.ini index 22127e0..4c831cf 100644 --- a/quality-flac.ini +++ b/quality-flac.ini @@ -11,4 +11,5 @@ location = ffmpeg [libconv] overwrite = false dry_run = true +threads = 6 diff --git a/small-mp3.ini b/small-mp3.ini index 53987c7..4ad4a8e 100644 --- a/small-mp3.ini +++ b/small-mp3.ini @@ -12,4 +12,5 @@ location = ffmpeg overwrite = true dry_run = false deduce_metadata = true +threads = 6