Restart QThread with GUI

Question:

I am using QThread to do some calculations in a separate Thread.
The Thread gets started by a button click, witch launches the function StartMeasurement().
The Thread can finish the process by itself (after finished the calculations)
and emits the PyQT Signal finished. Or the thread can be stopped by the User by the stopBtn click.

The terminate() function is working, but I get a lot of troubles when I try to start the thread again.

Is it recommendable to use the movetoThread() approach here?
Or how could I ensure that the thread is stopped correctly to enable a proper restart. (means, starting new!)

# starts the measurment in a Thread: StartMeasurement()
    def StartMeasurement(self):            

        self.thread = measure.CMeasurementThread(self.osziObj, self.genObj, self.measSetup)

        self.thread.newSample.connect(self.plotNewSample)
        self.thread.finished.connect(self.Done)
        self.stopBtn.clicked.connect(self.thread.terminate)
        self.stopBtn.clicked.connect(self.Stop)

        self.thread.start()
Asked By: MartinaW

||

Answers:

It’s not a problem. The general practice when working with QThread is to connect its finished() signal to the deleteLater() slot of the objects that have been moved to the separate thread via moveToThread(). It’s done in order to properly manage the memory when you then destroy your thread because it’s assumed that you will first quit the thread and then destroy its instance. 😉 This should tell you that stopping a thread has nothing to do with the destruction of those objects UNLESS you have established the connection I’ve described above.

It is perfectly fine to restart a thread IF you have stopped it properly using quit() and wait() to actually wait untill the stopping is completed.

However my advice is to not do that unless that extra thread has a huge impact on your application for some reason (highly unlikely with modern machines).

Instead of restarting the thread consider the following options:

  • implement a pause flag that just makes the thread run without doing anything if it’s set to true (I’ve used this example of mine many times to demonstrate such behaviour (check the worker.cpp and the doWork() function in particular) – it’s in C++ but it can be ported to PyQt in no time)
  • use QRunnable – its designed to run something and then (unless autoDelete is set to true) return to the thread pool. It’s really nice if you have tasks that occur every once in a while and you don’t need a constatly running separate thread. If you want to use signals and slots (to get the result of the calculation done inside the QRunnable::run() you will have to first inherit from QObject and then from QRunnable
  • Use futures (check the Qt Concurrent module)

I suggest that you first read the Example use cases for the various threading technologies Qt provides.

Answered By: rbaleksandar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.