From dafbc3b1c53be46fd2ea3e583800fac69d3807a9 Mon Sep 17 00:00:00 2001 From: Broque Thomas Date: Mon, 28 Jul 2025 16:28:15 -0700 Subject: [PATCH] good --- ui/pages/dashboard.py | 52 ++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/ui/pages/dashboard.py b/ui/pages/dashboard.py index 40e97cad..56ab768f 100644 --- a/ui/pages/dashboard.py +++ b/ui/pages/dashboard.py @@ -6,6 +6,7 @@ from PyQt6.QtGui import QFont, QPalette, QColor import time import asyncio import threading +from concurrent.futures import ThreadPoolExecutor, as_completed try: import resource HAS_RESOURCE = True @@ -36,6 +37,8 @@ class MetadataUpdateWorker(QThread): self.processed_count = 0 self.successful_count = 0 self.failed_count = 0 + self.max_workers = 4 # Same as your previous implementation + self.thread_lock = threading.Lock() def stop(self): self.should_stop = True @@ -63,30 +66,47 @@ class MetadataUpdateWorker(QThread): total_artists = len(self.artists) - for i, artist in enumerate(self.artists): + # Process artists in parallel using ThreadPoolExecutor + def process_single_artist(artist): + """Process a single artist and return results""" if self.should_stop: - break - + return None + artist_name = getattr(artist, 'title', 'Unknown Artist') - self.progress_updated.emit(artist_name, i, total_artists, (i / total_artists) * 100) try: success, details = self.update_artist_metadata(artist) - self.processed_count += 1 + return (artist_name, success, details) + except Exception as e: + return (artist_name, False, f"Error: {str(e)}") + + with ThreadPoolExecutor(max_workers=self.max_workers) as executor: + # Submit all tasks + future_to_artist = {executor.submit(process_single_artist, artist): artist + for artist in self.artists} + + # Process completed tasks as they finish + for future in as_completed(future_to_artist): + if self.should_stop: + break + + result = future.result() + if result is None: # Task was cancelled + continue + + artist_name, success, details = result - if success: - self.successful_count += 1 - else: - self.failed_count += 1 + with self.thread_lock: + self.processed_count += 1 + if success: + self.successful_count += 1 + else: + self.failed_count += 1 + # Emit progress and result signals + progress_percent = (self.processed_count / total_artists) * 100 + self.progress_updated.emit(artist_name, self.processed_count, total_artists, progress_percent) self.artist_updated.emit(artist_name, success, details) - - except Exception as e: - self.failed_count += 1 - self.artist_updated.emit(artist_name, False, f"Error: {str(e)}") - - # Small delay to prevent overwhelming the APIs - self.msleep(500) self.finished.emit(self.processed_count, self.successful_count, self.failed_count)