Move the thread pool to the shared object instance

This commit is contained in:
Veronica Berglyd Olsen
2023-09-10 17:46:35 +02:00
parent 16e2e0cc83
commit e210bca319
5 changed files with 32 additions and 9 deletions
+2 -2
View File
@@ -1139,7 +1139,7 @@ class GuiDocEditor(QPlainTextEdit):
if time() - self._lastEdit < 5 * self.wcInterval: if time() - self._lastEdit < 5 * self.wcInterval:
logger.debug("Running word counter") logger.debug("Running word counter")
self.mainGui.threadPool.start(self.wCounterDoc) SHARED.runInThreadPool(self.wCounterDoc)
return return
@@ -1192,7 +1192,7 @@ class GuiDocEditor(QPlainTextEdit):
logger.debug("Selection word counter is busy") logger.debug("Selection word counter is busy")
return return
self.mainGui.threadPool.start(self.wCounterSel) SHARED.runInThreadPool(self.wCounterSel)
return return
+9
View File
@@ -51,10 +51,19 @@ class GuiTextDocument(QTextDocument):
logger.debug("Delete: GuiTextDocument") logger.debug("Delete: GuiTextDocument")
return return
##
# Properties
##
@property @property
def syntaxHighlighter(self) -> GuiDocHighlighter: def syntaxHighlighter(self) -> GuiDocHighlighter:
"""Return the document's syntax highlighter object."""
return self._syntax return self._syntax
##
# Metods
##
def setTextContent(self, text: str, tHandle: str) -> None: def setTextContent(self, text: str, tHandle: str) -> None:
"""Set the text content of the document.""" """Set the text content of the document."""
self._syntax.setHandle(tHandle) self._syntax.setHandle(tHandle)
+1 -2
View File
@@ -30,7 +30,7 @@ from time import time
from pathlib import Path from pathlib import Path
from datetime import datetime from datetime import datetime
from PyQt5.QtCore import Qt, QTimer, QThreadPool, pyqtSlot from PyQt5.QtCore import Qt, QTimer, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QCursor, QIcon, QKeySequence from PyQt5.QtGui import QCloseEvent, QCursor, QIcon, QKeySequence
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialog, QFileDialog, QHBoxLayout, QMainWindow, QMessageBox, QShortcut, QDialog, QFileDialog, QHBoxLayout, QMainWindow, QMessageBox, QShortcut,
@@ -95,7 +95,6 @@ class GuiMain(QMainWindow):
logger.debug("Create: GUI") logger.debug("Create: GUI")
self.setObjectName("GuiMain") self.setObjectName("GuiMain")
self.threadPool = QThreadPool(self)
# System Info # System Info
# =========== # ===========
+15 -1
View File
@@ -29,7 +29,7 @@ from time import time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from pathlib import Path from pathlib import Path
from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtCore import QObject, QRunnable, QThreadPool, pyqtSignal
from PyQt5.QtWidgets import QMessageBox, QWidget from PyQt5.QtWidgets import QMessageBox, QWidget
from novelwriter.core.spellcheck import NWSpellEnchant from novelwriter.core.spellcheck import NWSpellEnchant
@@ -55,14 +55,23 @@ class SharedData(QObject):
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
# Objects
self._gui = None self._gui = None
self._theme = None self._theme = None
self._project = None self._project = None
self._spelling = None self._spelling = None
# Settings
self._lockedBy = None self._lockedBy = None
self._alert = None self._alert = None
self._idleTime = 0.0 self._idleTime = 0.0
self._idleRefTime = time() self._idleRefTime = time()
# Threading
self._threadPool = QThreadPool(self)
self._threadPool.setMaxThreadCount(5)
return return
## ##
@@ -199,6 +208,11 @@ class SharedData(QObject):
self.projectStatusChanged.emit(state) self.projectStatusChanged.emit(state)
return return
def runInThreadPool(self, runnable: QRunnable, priority: int = 0) -> None:
"""Queue a runnable in the application thread pool."""
self._threadPool.start(runnable, priority=priority)
return
## ##
# Alert Boxes # Alert Boxes
## ##
+5 -4
View File
@@ -1100,13 +1100,14 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m
def __init__(self): def __init__(self):
self._objID = None self._objID = None
def start(self, runObj): def start(self, runObj, priority=0):
self._objID = id(runObj) self._objID = id(runObj)
def objectID(self): def objectID(self):
return self._objID return self._objID
nwGUI.threadPool = MockThreadPool() threadPool = MockThreadPool()
monkeypatch.setattr(SHARED, "_threadPool", threadPool)
nwGUI.docEditor.wcTimerDoc.blockSignals(True) nwGUI.docEditor.wcTimerDoc.blockSignals(True)
nwGUI.docEditor.wcTimerSel.blockSignals(True) nwGUI.docEditor.wcTimerSel.blockSignals(True)
@@ -1145,7 +1146,7 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m
# Run the full word counter # Run the full word counter
nwGUI.docEditor._runDocCounter() nwGUI.docEditor._runDocCounter()
assert nwGUI.threadPool.objectID() == id(nwGUI.docEditor.wCounterDoc) assert SHARED._threadPool.objectID() == id(nwGUI.docEditor.wCounterDoc)
nwGUI.docEditor.wCounterDoc.run() nwGUI.docEditor.wCounterDoc.run()
# nwGUI.docEditor._updateDocCounts(cC, wC, pC) # nwGUI.docEditor._updateDocCounts(cC, wC, pC)
@@ -1161,7 +1162,7 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, nwGUI, projPath, ipsumText, m
# Run the selection word counter # Run the selection word counter
nwGUI.docEditor._runSelCounter() nwGUI.docEditor._runSelCounter()
assert nwGUI.threadPool.objectID() == id(nwGUI.docEditor.wCounterSel) assert SHARED._threadPool.objectID() == id(nwGUI.docEditor.wCounterSel)
nwGUI.docEditor.wCounterSel.run() nwGUI.docEditor.wCounterSel.run()
# nwGUI.docEditor._updateSelCounts(cC, wC, pC) # nwGUI.docEditor._updateSelCounts(cC, wC, pC)