我尝试使用多处理来创建非阻塞GUI。函数Multiprocessing.Pool.appy_async()允许添加回调函数,从而在完成耗时的操作之后轻松更新主GUI。但是,下面的代码在单击button1时仍然会阻塞。我该如何修改它,以便在执行button1回调时,button2仍然能够响应。我正在运行python 2.7和多处理0.70a1。from PySide.QtCore import *from PySide.QtGui import *import multiprocessingimport timeimport sysdef f(x): '''This is a time-intensive function ''' y = x*x time.sleep(2) return yclass MainWindow(QMainWindow): #You can only add menus to QMainWindows def __init__(self): super(MainWindow, self).__init__() self.pool = multiprocessing.Pool(processes=4) button1 = QPushButton('Connect', self) button1.clicked.connect(self.apply_connection) button2 = QPushButton('Test', self) button2.clicked.connect(self.apply_test) self.text = QTextEdit() vbox1 = QVBoxLayout() vbox1.addWidget(button1) vbox1.addWidget(button2) vbox1.addWidget(self.text) myframe = QFrame() myframe.setLayout(vbox1) self.setCentralWidget(myframe) self.show() #display and activate focus self.raise_() def apply_connection(self): result = self.pool.apply_async(f, [10], callback=self.update_gui) result.get(3) def update_gui(self, result): self.text.append('Applied connection. Result = %d\n' % result) def apply_test(self): self.text.append('Testing\n')if __name__ == '__main__': app = QApplication(sys.argv) gui = MainWindow() app.exec_()
添加回答
举报
0/150
提交
取消