Skip to content

Commit

Permalink
refactor(connections): use blocking queued connections for worker sig…
Browse files Browse the repository at this point in the history
…nals(Resolve some stability issues)
  • Loading branch information
chenmozhijin committed Oct 26, 2024
1 parent e375826 commit e0c2d38
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion LDDC/backend/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def handle_task(self, task: dict) -> None:
info["duration"] = info["duration"] / 1000
worker = AutoLyricsFetcher(info,
source=[Source[s] for s in cfg["desktop_lyrics_sources"]], taskid=self.taskid)
worker.signals.result.connect(self.handle_fetch_result)
worker.signals.result.connect(self.handle_fetch_result, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)
else:
# 没有标题无法自动获取歌词
Expand Down
7 changes: 4 additions & 3 deletions LDDC/backend/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class CheckUpdateSignal(QObject):


class CheckUpdate(QRunnable):

def __init__(self, is_auto: bool, name: str, repo: str, version: str) -> None:
super().__init__()
self.isAuto = is_auto
Expand Down Expand Up @@ -152,8 +153,8 @@ def run(self) -> None:
else:
self.search_task[task_id] = None
worker = SearchWorker(task_id, self.keyword, self.search_type, source, self.page, self.info)
worker.signals.result.connect(self.handle_search_result)
worker.signals.error.connect(self.handle_search_error)
worker.signals.result.connect(self.handle_search_result, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.handle_search_error, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

if self.search_task_finished != len(self.search_task):
Expand Down Expand Up @@ -381,7 +382,7 @@ def fetch_next_lyrics(self) -> None:
info = self.song_infos[self.current_index]
self.current_index += 1
worker = AutoLyricsFetcher(info, self.min_score, self.source)
worker.signals.result.connect(self.handle_fetch_result)
worker.signals.result.connect(self.handle_fetch_result, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

def run(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion LDDC/res/resource_rc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Resource object code (Python 3)
# Created by: object code
# Created by: The Resource Compiler for Qt version 6.7.3
# Created by: The Resource Compiler for Qt version 6.8.0
# WARNING! All changes made in this file will be lost!

from PySide6 import QtCore
Expand Down
10 changes: 5 additions & 5 deletions LDDC/view/local_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-only
import os

from PySide6.QtCore import Slot
from PySide6.QtCore import Qt, Slot
from PySide6.QtWidgets import (
QCheckBox,
QComboBox,
Expand Down Expand Up @@ -131,10 +131,10 @@ def start_cancel_button_clicked(self) -> None:
"source": source,
},
)
self.worker.signals.error.connect(self.worker_error)
self.worker.signals.finished.connect(self.worker_finished)
self.worker.signals.massage.connect(self.worker_massage)
self.worker.signals.progress.connect(self.change_progress)
self.worker.signals.error.connect(self.worker_error, Qt.ConnectionType.BlockingQueuedConnection)
self.worker.signals.finished.connect(self.worker_finished, Qt.ConnectionType.BlockingQueuedConnection)
self.worker.signals.massage.connect(self.worker_massage, Qt.ConnectionType.BlockingQueuedConnection)
self.worker.signals.progress.connect(self.change_progress, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.startOnReservedThread(self.worker)

@Slot(str)
Expand Down
26 changes: 13 additions & 13 deletions LDDC/view/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ def search(self) -> None:
self.taskid["results_table"] += 1
self.search_info = {'keyword': keyword, 'search_type': SearchType(self.search_type_comboBox.currentIndex()), 'source': self.get_source(), 'page': 1}
worker = SearchWorker(self.taskid["results_table"], keyword, SearchType(self.search_type_comboBox.currentIndex()), self.get_source(), 1)
worker.signals.result.connect(self.search_result_slot)
worker.signals.error.connect(self.search_error_slot)
worker.signals.result.connect(self.search_result_slot, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.search_error_slot, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)
self.results_tableWidget.setRowCount(0)
self.results_tableWidget.setColumnCount(0)
Expand Down Expand Up @@ -227,8 +227,8 @@ def update_preview_lyric(self, info: dict | None = None) -> None:
"lyrics_format": LyricsFormat(self.lyricsformat_comboBox.currentIndex()),
"id": self.taskid["update_preview_lyric"],
"offset": self.offset_spinBox.value()})
worker.signals.result.connect(self.update_preview_lyric_result_slot)
worker.signals.error.connect(self.update_preview_lyric_error_slot)
worker.signals.result.connect(self.update_preview_lyric_result_slot, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.update_preview_lyric_error_slot, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

self.preview_lyric_result = None
Expand Down Expand Up @@ -340,8 +340,8 @@ def get_songlist_result(self, result_type: str, info: dict) -> None:
else:
msg = "Unknown result_type"
raise ValueError(msg)
worker.signals.result.connect(self.show_songlist_result)
worker.signals.error.connect(self.get_songlist_error)
worker.signals.result.connect(self.show_songlist_result, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.get_songlist_error, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)
self.results_tableWidget.setRowCount(0)
self.results_tableWidget.setColumnCount(0)
Expand Down Expand Up @@ -378,8 +378,8 @@ def search_lyrics(self, info: dict) -> None:
worker = SearchWorker(self.taskid["results_table"],
f"{get_artist_str(info.get('artist'), '、')} - {info['title'].strip()}",
SearchType.LYRICS, info['source'], 1, info)
worker.signals.result.connect(self.search_lyrics_result_slot)
worker.signals.error.connect(self.search_lyrics_error_slot)
worker.signals.result.connect(self.search_lyrics_result_slot, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.search_lyrics_error_slot, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

@Slot(QModelIndex)
Expand Down Expand Up @@ -488,8 +488,8 @@ def results_table_scroll_changed(self) -> None:
self.search_info['search_type'],
self.search_info['source'],
self.search_info['page'] + 1)
worker.signals.result.connect(self.search_nextpage_result_slot)
worker.signals.error.connect(self.search_nextpage_error)
worker.signals.result.connect(self.search_nextpage_result_slot, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(self.search_nextpage_error, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)


Expand Down Expand Up @@ -642,8 +642,8 @@ def get_list_lyrics_update(count: int | str, result: dict | None = None) -> None
self.get_list_lyrics_box.show()
self.get_list_lyrics_box.setEnabled(True)

worker.signals.result.connect(get_list_lyrics_update)
worker.signals.error.connect(get_list_lyrics_update)
worker.signals.result.connect(get_list_lyrics_update, Qt.ConnectionType.BlockingQueuedConnection)
worker.signals.error.connect(get_list_lyrics_update, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

@Slot()
Expand Down Expand Up @@ -794,7 +794,7 @@ def dropEvent(self, event: QDropEvent) -> None:
MsgBox.warning(self, "警告", "没有获取到歌曲标题,无法自动搜索")

worker = AutoLyricsFetcher(song, taskid=tuple(self.taskid.values()), return_search_result=True)
worker.signals.result.connect(self.auto_fetch_slot)
worker.signals.result.connect(self.auto_fetch_slot, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)
self.preview_lyric_result = None
self.preview_plainTextEdit.setPlainText(self.tr("正在自动获取 {0} 的歌词...").format(
Expand Down
2 changes: 1 addition & 1 deletion LDDC/view/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def language_comboBox_changed(self, index: int) -> None:

@Slot()
def update_cache_size(self) -> None:
self.cache_size_label.setText(self.tr("缓存大小:") + f" {cache.volume() / 1000000 } MB")
self.cache_size_label.setText(self.tr("缓存大小:") + f" {cache.volume() / 1000000} MB")

@Slot()
def clear_cache(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion LDDC/view/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ def show_new_version_dialog(name: str, repo: str, new_version: str, body: str) -

def check_update(is_auto: bool, name: str, repo: str, version: str) -> None:
worker = CheckUpdate(is_auto, name, repo, version)
worker.signals.show_new_version_dialog.connect(show_new_version_dialog)
worker.signals.show_new_version_dialog.connect(show_new_version_dialog, Qt.ConnectionType.BlockingQueuedConnection)
threadpool.start(worker)

0 comments on commit e0c2d38

Please sign in to comment.