From 3f91f5c50633c62174265f3abe3cef60f46c2179 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 10 Sep 2023 18:15:16 +0200 Subject: [PATCH] Use the global thread pool, like the docs suggest --- novelwriter/__init__.py | 2 +- novelwriter/shared.py | 9 +++------ tests/test_gui/test_gui_doceditor.py | 8 ++++---- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/novelwriter/__init__.py b/novelwriter/__init__.py index 3b49359a..b3b39028 100644 --- a/novelwriter/__init__.py +++ b/novelwriter/__init__.py @@ -73,7 +73,7 @@ logger = logging.getLogger(__name__) # Main Program ## -# Global config singleton +# Global config and data singletons CONFIG = Config() SHARED = SharedData() diff --git a/novelwriter/shared.py b/novelwriter/shared.py index b5e6d4fd..8ee3099a 100644 --- a/novelwriter/shared.py +++ b/novelwriter/shared.py @@ -68,10 +68,6 @@ class SharedData(QObject): self._idleTime = 0.0 self._idleRefTime = time() - # Threading - self._threadPool = QThreadPool(self) - self._threadPool.setMaxThreadCount(5) - return ## @@ -138,7 +134,8 @@ class SharedData(QObject): self._gui = gui self._theme = theme self._resetProject() - logger.debug("SharedData instance initialised") + logger.debug("Ready: SharedData") + logger.debug("Thread Pool Max Count: %d", QThreadPool.globalInstance().maxThreadCount()) return def openProject(self, path: str | Path, clearLock: bool = False) -> bool: @@ -210,7 +207,7 @@ class SharedData(QObject): def runInThreadPool(self, runnable: QRunnable, priority: int = 0) -> None: """Queue a runnable in the application thread pool.""" - self._threadPool.start(runnable, priority=priority) + QThreadPool.globalInstance().start(runnable, priority=priority) return ## diff --git a/tests/test_gui/test_gui_doceditor.py b/tests/test_gui/test_gui_doceditor.py index 276bafa5..faa75027 100644 --- a/tests/test_gui/test_gui_doceditor.py +++ b/tests/test_gui/test_gui_doceditor.py @@ -24,7 +24,7 @@ import pytest from mocked import causeOSError from tools import C, buildTestProject -from PyQt5.QtCore import Qt +from PyQt5.QtCore import QThreadPool, Qt from PyQt5.QtGui import QTextBlock, QTextCursor, QTextOption from PyQt5.QtWidgets import QAction, qApp @@ -1107,7 +1107,7 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m return self._objID threadPool = MockThreadPool() - monkeypatch.setattr(SHARED, "_threadPool", threadPool) + monkeypatch.setattr(QThreadPool, "globalInstance", lambda *a: threadPool) nwGUI.docEditor.wcTimerDoc.blockSignals(True) nwGUI.docEditor.wcTimerSel.blockSignals(True) @@ -1146,7 +1146,7 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m # Run the full word counter nwGUI.docEditor._runDocCounter() - assert SHARED._threadPool.objectID() == id(nwGUI.docEditor.wCounterDoc) + assert threadPool.objectID() == id(nwGUI.docEditor.wCounterDoc) nwGUI.docEditor.wCounterDoc.run() # nwGUI.docEditor._updateDocCounts(cC, wC, pC) @@ -1162,7 +1162,7 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m # Run the selection word counter nwGUI.docEditor._runSelCounter() - assert SHARED._threadPool.objectID() == id(nwGUI.docEditor.wCounterSel) + assert threadPool.objectID() == id(nwGUI.docEditor.wCounterSel) nwGUI.docEditor.wCounterSel.run() # nwGUI.docEditor._updateSelCounts(cC, wC, pC)