Running measurements in separate worker thread.

This commit is contained in:
jupfi 2024-05-19 15:46:26 +02:00
parent 92a6e31c1a
commit 27865aa6d4

View file

@ -4,6 +4,7 @@ Careful - this is not the base class for the spectrometer submodules, but the co
""" """
import logging import logging
from PyQt6.QtCore import QObject, pyqtSignal, QThread
from nqrduck.module.module_controller import ModuleController from nqrduck.module.module_controller import ModuleController
from nqrduck.core.main_controller import MainController from nqrduck.core.main_controller import MainController
from nqrduck_spectrometer.base_spectrometer import BaseSpectrometer from nqrduck_spectrometer.base_spectrometer import BaseSpectrometer
@ -17,6 +18,8 @@ class SpectrometerController(ModuleController):
def __init__(self, module): def __init__(self, module):
"""This method initializes the controller.""" """This method initializes the controller."""
super().__init__(module) super().__init__(module)
self.measurement_thread = None
self.measurement_worker = None
def _load_spectrometer_modules(self) -> None: def _load_spectrometer_modules(self) -> None:
"""This method loads the spectrometer (sub-)modules and adds them to the spectrometer model.""" """This method loads the spectrometer (sub-)modules and adds them to the spectrometer model."""
@ -54,7 +57,7 @@ class SpectrometerController(ModuleController):
""" """
# This signal starts a measurement # This signal starts a measurement
if key == "start_measurement": if key == "start_measurement":
self.on_measurement_start() self.start_measurement_in_thread()
# This signal sets the frequency # This signal sets the frequency
elif key == "set_frequency": elif key == "set_frequency":
self.module.model.active_spectrometer.controller.set_frequency(value) self.module.model.active_spectrometer.controller.set_frequency(value)
@ -85,3 +88,29 @@ class SpectrometerController(ModuleController):
self.module.model.active_spectrometer, self.module.model.active_spectrometer,
) )
self.module.model.active_spectrometer.controller.start_measurement() self.module.model.active_spectrometer.controller.start_measurement()
def start_measurement_in_thread(self):
"""This method starts the measurement in a separate QThread."""
self.measurement_thread = QThread()
self.measurement_worker = MeasurementWorker(self)
self.measurement_worker.moveToThread(self.measurement_thread)
self.measurement_thread.started.connect(self.measurement_worker.run)
self.measurement_worker.finished.connect(self.measurement_thread.quit)
self.measurement_worker.finished.connect(self.measurement_worker.deleteLater)
self.measurement_thread.finished.connect(self.measurement_thread.deleteLater)
self.measurement_thread.start()
class MeasurementWorker(QObject):
"""Worker class to run the measurement in a separate thread."""
finished = pyqtSignal()
def __init__(self, controller):
"""Initialize the worker."""
super().__init__()
self.controller = controller
def run(self):
"""Run the measurement."""
self.controller.on_measurement_start()
self.finished.emit()