From d2b21ca937f2178e668724bdf85f291cb439d9de Mon Sep 17 00:00:00 2001 From: nathom Date: Tue, 13 Apr 2021 10:27:37 -0700 Subject: [PATCH] Repace threading with ThreadPoolExecutor --- streamrip/downloader.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/streamrip/downloader.py b/streamrip/downloader.py index 78cd5b2..47dec54 100644 --- a/streamrip/downloader.py +++ b/streamrip/downloader.py @@ -7,7 +7,7 @@ import os import re import shutil import subprocess -import threading +import concurrent.futures from pprint import pformat from tempfile import gettempdir from typing import Any, Generator, Iterable, Union @@ -622,20 +622,15 @@ class Tracklist(list): target = self._download_item if kwargs.get("concurrent_downloads", True): - processes = [] - for item in self: - proc = threading.Thread( - target=target, args=(item,), kwargs=kwargs, daemon=True - ) - proc.start() - processes.append(proc) - - try: - for proc in processes: - proc.join() - except (KeyboardInterrupt, SystemExit): - click.echo("Aborted!") - exit() + # Tidal errors out with unlimited concurrency + max_workers = 15 if self.client.source == 'tidal' else None + with concurrent.futures.ThreadPoolExecutor(max_workers) as executor: + futures = [executor.submit(target, item, **kwargs) for item in self] + try: + concurrent.futures.wait(futures) + except (KeyboardInterrupt, SystemExit): + executor.shutdown() + exit("Aborted!") else: for item in self: