Pause worker thread and wait for event from main thread Pause worker thread and wait for event from main thread multithreading multithreading

Pause worker thread and wait for event from main thread


Below is a simple demo based on the code in your question which does what you want. There is not much to say about it, really, other than that you need to communicate between the worker and the main thread via signals (in both directions). The finished signal is used to quit the thread, which will stop the warning message QThread: "Destroyed while thread is still running" being shown.

The reason why you are seeing the error:

TypeError: connect() slot argument should be a callable or a signal, not `NoneType'

is because you are trying to connect a signal with the return value of a function (which is None), rather than the function object itself. You must always pass a python callable object to the connect method - anything else will raise a TypeError.

Please run the script below and confirm that it works as expected. Hopefully it should be easy to see how to adapt it to work with your real code.

from PyQt4.QtCore import *from PyQt4.QtGui import *class Cont(QWidget):    confirmed = pyqtSignal()    def __init__(self):        super(Cont, self).__init__()        self.thread = QThread()        self.worker = Worker()        self.worker.moveToThread(self.thread)        self.worker.bv.connect(self.bv_test)        self.worker.finished.connect(self.thread.quit)        self.confirmed.connect(self.worker.process_two)        self.thread.started.connect(self.worker.process_one)        self.thread.start()    def bv_test(self, n):        k = QMessageBox(self)        k.setAttribute(Qt.WA_DeleteOnClose)        k.setText('Quantity: {}'.format(n))        k.setStandardButtons(QMessageBox.Yes | QMessageBox.No)        if k.exec_() == QMessageBox.Yes:            self.confirmed.emit()        else:            self.thread.quit()class Worker(QObject):    bv = pyqtSignal(int)    finished = pyqtSignal()    def process_two(self):        print('process: two: started')        QThread.sleep(1)        print('process: two: finished')        self.finished.emit()    def process_one(self):        print('process: one: started')        QThread.sleep(1)        self.bv.emit(99)        print('process: one: finished')app = QApplication([''])win = Cont()win.setGeometry(100, 100, 100, 100)win.show()app.exec_()


If you want the thread to wait for the action, connect to a signal from the thread using

PyQt4.QtCore.Qt.BlockingQueuedConnection

as flag.

Now I do not understand why you need threading if you let them wait, which brings in a lot of complexity. For me the better solution would be to cut the task you want to perform in the threads in smaller pieces. Each time a piece is ready, you can ask if the user wants the next too.