From 4a6813d33ae09132723fe9fff41cdecade49017f Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Wed, 1 Dec 2021 15:48:29 +0100 Subject: [PATCH] Encode different episodes simultaneously MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This results in less nice log output, but if it’s faster then who cares? --- encode.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/encode.py b/encode.py index 0ea666e..7255560 100755 --- a/encode.py +++ b/encode.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 import base64 +import concurrent.futures +import multiprocessing import os -import threading import xml.etree.ElementTree as ET from datetime import datetime from subprocess import run @@ -77,9 +78,9 @@ def encode_episode(podcast, episode, format): audio.save() - print(f" {format}", end="", flush=True) + print(f"[✔️] {episode['file_base']}.{format}") else: - print(f" ({format})", end="", flush=True) + print(f"[⏭️] {episode['file_base']}.{format}") os.makedirs("static/episodes", exist_ok=True) @@ -95,6 +96,8 @@ podcast = { "poster": "static" + urlparse(channel.find("image").find("url").text).path, } +pool = concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) + for item in channel.findall("item"): episode = { "title": item.find("title").text, @@ -114,18 +117,7 @@ for item in channel.findall("item"): )[0], } - print(episode["file_base"], end="", flush=True) - - threads = [] - for format in common.FORMATS.items(): - thread = threading.Thread( - target=encode_episode, args=(podcast, episode, format), daemon=True - ) - thread.start() - threads.append(thread) + pool.submit(encode_episode, podcast, episode, format) - for thread in threads: - thread.join() - - print() +pool.shutdown(wait=True)