gpt4 book ai didi

python - 暂停工作线程并等待来自主线程的事件

转载 作者:太空狗 更新时间:2023-10-30 00:47:34 26 4
gpt4 key购买 nike

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

那部分看起来像这样:

    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

我们的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)

并且 self.ui 设置为 Ui_MainWindow():

class Cont(QMainWindow):
def __init__(self, parent=None):
QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)

有一个特定的基础在继续之前将数据发送给用户(返回到主窗口)(在本例中,是一个带有两个按钮的弹出窗口):

#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_()

我怎样才能在某个点暂停线程,显示窗口(我相信它会返回到主线程)并返回到工作线程,传递按钮点击事件?

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

编辑阅读这个问题后:Similar question ,我将代码更改为:

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

这是 Worker 类:

class Worker(QObject):

#Signals
bv = pyqtSignal(int)

def process(self):
self.bv.emit(99)

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

TypeError: connect() 槽参数应该是可调用的或信号,而不是“NoneType”

最佳答案

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

您看到错误的原因:

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

是因为您试图将信号与函数的返回 值(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_()

关于python - 暂停工作线程并等待来自主线程的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48643123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com