问题
I am using QThread to do some calculations in a seperate Thread. The Thread get's 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()
回答1:
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 totrue
(I've used this example of mine many times to demonstrate such behaviour (check theworker.cpp
and thedoWork()
function in particular) - it's in C++ but it can be ported toPyQt
in no time) - use
QRunnable
- its designed to run something and then (unlessautoDelete
is set totrue
) 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 theQRunnable::run()
you will have to first inherit fromQObject
and then fromQRunnable
- Use futures (check the Qt Concurrent module)
I suggest that you first read the Example use cases for the various threading technologies Qt provides.
来源:https://stackoverflow.com/questions/44006024/restart-qthread-with-gui