暂停工作线程并等待主线程中的事件

Oni*_*lol 5 python multithreading signals-slots pyqt4 python-2.7

我们有一个执行不同查询的应用程序.它最多启动四个线程,并在它们上运行提取.

那部分看起来像这样:

    if len(self.threads) == 4:
        self.__maxThreadsMsg(base)
        return False
    else:
        self.threads.append(Extractor(self.ui, base))
        self.threads[-1].start()
        self.__extractionMsg(base)
        return True
Run Code Online (Sandbox Code Playgroud)

我们的Extractor类继承QThread:

class Extractor(QThread):
    def init(self, ui, base):
        QThread.__init__(self)
        self.ui = ui
        self.base = base

    def run(self):
        self.run_base(base)
Run Code Online (Sandbox Code Playgroud)

self.ui设置为Ui_MainWindow():

class Cont(QMainWindow):
    def __init__(self, parent=None):
        QWidget.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
Run Code Online (Sandbox Code Playgroud)

在继续之前,有一个特定的基础将数据发送给用户(返回主窗口)(在这种情况下,带有两个按钮的弹出窗口):

#This code is in the main file inside a method, not in the Extractor class
msg_box = QMessagebox()
msg_box.setText('Quantity in base: '.format(n))
msg_box.setInformativeText('Would you like to continue?')
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
signal = msg_box.exec_()
Run Code Online (Sandbox Code Playgroud)

如何在特定点暂停线程,显示窗口(我相信将返回主线程)并返回工作线程,传递按钮点击事件?

我读了一些关于信号的内容,但它似乎令人困惑,因为这是我第一次处理线程.

编辑:阅读此问题后:类似问题,我将代码更改为:

Cont类的内部方法

thread = QThread(self)
worker = Worker()

worker.moveToThread(thread)
worker.bv.connect(self.bv_test)

thread.started.connect(worker.process()) # This, unlike in the linked question.. 
#doesn't work if I remove the parentheses of the process function. 
#If I remove it, nothing happens and I get QThread: "Destroyed while thread is still running"

thread.start()

@pyqtSlot(int)
def bv_test(self, n):
    k = QMessageBox()
    k.setText('Quantity: {}'.format(n))
    k.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    ret = k.exec_()
    return ret
Run Code Online (Sandbox Code Playgroud)

这是Worker班级:

class Worker(QObject):

    #Signals
    bv = pyqtSignal(int)

    def process(self):
        self.bv.emit(99)
Run Code Online (Sandbox Code Playgroud)

现在我只需要弄清楚如何将ret值发送回工作线程,以便它启动第二个进程.我也一直收到这个错误:

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

ekh*_*oro 6

下面是一个基于您的问题中的代码的简单演示,它可以满足您的需求.除此之外,没有什么可说的,除了你需要通过信号(两个方向)在工作者和主线程之间进行通信.该finished信号用于退出线程,这将停止显示警告消息QThread: "Destroyed while thread is still running".

您看到错误的原因:

TypeError: connect() slot argument should be a callable or a signal, not `NoneType'
Run Code Online (Sandbox Code Playgroud)

是因为您正在尝试使用函数的返回值(即None)来连接信号,而不是函数对象本身.您必须始终将python可调用对象传递给该connect方法 - 其他任何东西都会引发一个TypeError.

请运行下面的脚本并确认它按预期工作.希望应该很容易看到如何使其适应您的真实代码.

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_()
Run Code Online (Sandbox Code Playgroud)