Repace threading with ThreadPoolExecutor

This commit is contained in:
nathom 2021-04-13 10:27:37 -07:00
parent e28e8b31ea
commit d2b21ca937

View file

@ -7,7 +7,7 @@ import os
import re import re
import shutil import shutil
import subprocess import subprocess
import threading import concurrent.futures
from pprint import pformat from pprint import pformat
from tempfile import gettempdir from tempfile import gettempdir
from typing import Any, Generator, Iterable, Union from typing import Any, Generator, Iterable, Union
@ -622,20 +622,15 @@ class Tracklist(list):
target = self._download_item target = self._download_item
if kwargs.get("concurrent_downloads", True): if kwargs.get("concurrent_downloads", True):
processes = [] # Tidal errors out with unlimited concurrency
for item in self: max_workers = 15 if self.client.source == 'tidal' else None
proc = threading.Thread( with concurrent.futures.ThreadPoolExecutor(max_workers) as executor:
target=target, args=(item,), kwargs=kwargs, daemon=True futures = [executor.submit(target, item, **kwargs) for item in self]
) try:
proc.start() concurrent.futures.wait(futures)
processes.append(proc) except (KeyboardInterrupt, SystemExit):
executor.shutdown()
try: exit("Aborted!")
for proc in processes:
proc.join()
except (KeyboardInterrupt, SystemExit):
click.echo("Aborted!")
exit()
else: else:
for item in self: for item in self: