2016-11-27 35 views
1

我一直在尝试使用多进程运行命令,因为在使用while循环时GUI冻结。我需要调用pyqt4类中的函数。或者更好的方式来处理多进程将Qthread帮助我?我搜索了很多教程,但我无法弄清楚我该如何做到这一点。PyQt4如何通过单击pushButton来使用多处理

我试过这样,它工作正常。问题是我不能得到传递给函数的QeditText的输入,如果有一种方法,我可以然后它会为我想要做的。

import sys 
import multiprocessing 
import time 
from PyQt4 import QtCore, QtGui 
from form import Ui_Dialog 


def worker(): 
    t = MyDialog() 
    name = multiprocessing.current_process().name 
    print name, 'Starting', t.self.ui.rtmpIN.toPlainText() 
    time.sleep(2) 
    print name, 'Exiting' 

class MyDialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_Dialog() 
     self.ui.setupUi(self) 
     self.ui.startButton.clicked.connect(self.start) 
     self.ui.stopButton.clicked.connect(self.stop) 
     self.ui.comboBox.addItem("player 1") 
     self.ui.comboBox.addItem("player 2") 
     self.ui.comboBox.addItem("player 3") 
     #self.ui.comboBox.currentIndexChanged.connect(self.selectionchange) 

    def selectionchange(self,i): 
     print self.ui.comboBox.currentText() 

    def start(self): 
     worker_2 = multiprocessing.Process(target=worker) # use default name 
     worker_2.start() 
     print "in: ", self.ui.rtmpIN.toPlainText() 
     print "out: ", self.ui.outPUT.toPlainText() 
     print str(self.ui.comboBox.currentText()) 

     if self.ui.quialityBox.isChecked(): 
      q = "Streaming started" + "\n" + "quality: " + self.ui.Setquality.toPlainText() + "\n" + "player: " + str(self.ui.comboBox.currentText()) 
      self.ui.theLog.append(q) 
      #print self.ui.Setquality.toPlainText() 
     else: 
      p = "Streaming" + "\n" + "player: " + str(self.ui.comboBox.currentText()) + "\n" 
      self.ui.theLog.append(p) 

    def stop(self): 
     print 'stop pressed.' 
     self.close() 




if __name__ == "__main__": 
     app = QtGui.QApplication(sys.argv) 
     myapp = MyDialog() 
     myapp.show() 
     sys.exit(app.exec_()) 

我需要能够从工作人员功能中获取数据,有没有办法?

print "in: ", self.ui.rtmpIN.toPlainText() 
print "out: ", self.ui.outPUT.toPlainText() 

编辑:这里忘了form.py是 http://pastebin.com/HksuSjkt

+0

请加form.py – eyllanesc

+0

@eyllaneschere的形式是http://pastebin.com/HksuSjkt – Slightz

+0

@Sligthz我创建了Qthread进程https://github.com/CodeHuntersLab/CuriElements/blob/master/CuriElements/soundthread。 py – eyllanesc

回答

1

这是我的解决方案:

from PyQt4.QtCore import QThread 

class Worker(QThread): 
    def __init__(self, parent=None): 
     super(Worker, self).__init__(parent) 
     self.textin = "" 
     self.textout = "" 
     self.okay = True 

    def setTextIn(self, text): 
     self.textin = text 

    def setTextOut(self, text): 
     self.textout = text 

    def run(self): 
     while self.okay: 
      print('IN:' + self.textin) 
      print('OUT:' + self.textout) 
      time.sleep(2) 

    def stop(self): 
     self.okay = False 


class MyDialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.worker = Worker(self) 
     self.ui = Ui_Dialog() 
     self.ui.setupUi(self) 
     self.ui.rtmpIN.textChanged.connect(self.changeText) 
     self.ui.outPUT.textChanged.connect(self.changeText) 
     self.ui.startButton.clicked.connect(self.start) 
     self.ui.stopButton.clicked.connect(self.stop) 
     self.ui.comboBox.addItem("player 1") 
     self.ui.comboBox.addItem("player 2") 
     self.ui.comboBox.addItem("player 3") 

    def selectionchange(self,i): 
     print(self.ui.comboBox.currentText()) 

    def start(self): 
     self.worker.start() 
     print("in: "+self.ui.rtmpIN.toPlainText()) 
     print("out: "+self.ui.outPUT.toPlainText()) 
     print(self.ui.comboBox.currentText()) 

     if self.ui.quialityBox.isChecked(): 
      q = "Streaming started" + "\n" + "quality: " + self.ui.Setquality.toPlainText() + "\n" + "player: " + str(self.ui.comboBox.currentText()) 
      self.ui.theLog.append(q) 
     else: 
      p = "Streaming" + "\n" + "player: " + str(self.ui.comboBox.currentText()) + "\n" 
      self.ui.theLog.append(p) 

    def changeText(self): 
     self.worker.setTextIn(self.ui.rtmpIN.toPlainText()) 
     self.worker.setTextOut(self.ui.outPUT.toPlainText()) 

    def stop(self): 
     self.worker.stop() 
     self.worker.quit() 
     self.worker.wait() 
     print('stop pressed.') 
     self.close() 

    def closeEvent(self, event): 
     self.worker.stop() 
     self.worker.quit() 
     self.worker.wait() 
     QtGui.QDialog.closeEvent(self, event) 


if __name__ == "__main__": 
     app = QtGui.QApplication(sys.argv) 
     myapp = MyDialog() 
     myapp.show() 
     sys.exit(app.exec_()) 

输出:

enter image description here

+0

我得到一个错误NameError:名称'QThread'未定义 nvm,我加入 从PyQt4.QtCore进口的QThread 但现在当我点击停止PI收到其他错误 超()的closeEvent(事件) 类型错误:超()至少需要1个参数(0给出) – Slightz

+0

@Slightz更新我解决方案 – eyllanesc

+0

@Slightz完整代码:http://pastebin.com/jQ2vFc4t – eyllanesc

0

我不是一个在PyQt的专家,但我认为你把一个条目部件中提到您form.py eyllanesc,我可以看到你导入。

你使用特殊变量来包含UI等字符串的原因之一是它驻留的类占用了一个指定的内存空间(当涉及到范围等时)。

当涉及到多处理时,内存空间不再共享,您无法再访问此变量。如果你使用multiTHREADING,它是一个新的线程,但在同一个进程中,这意味着它可以访问你想要的Qt字符串。

的语法是为了这个目的相同

import threading 

def foo(): pass 

t = threading.Thread(target=foo, 
        args=[]) 

t.daemon = True 

t.start() 

了解更多关于线程here

+1

是的,我尝试过线程模块,但是我会去杀死那个线程吗?例如,应用程序是使用FFmpeg restream,我使用paramiko连接到ssh,然后执行命令。因此,有时流是坏的,我需要停下来编辑。如果我每次关闭GUI,我将不得不再次设置rtmp信息,所以我想断开当我单击停止并重新连接 – Slightz

相关问题