Slightz Slightz - 6 days ago 4
Python Question

PyQt4 how can i use multiprocessing by clicking a pushButton

I have been trying to run a command using multiprocess since the GUI freezes when using a while loop. I need to call the function inside of pyqt4 class. Or a better way to handle multiprocess will Qthread help me? I have search many tutorials, but I cannot figure out how I can do this.

I tried it like this, which works fine. The problem is I cannot get the input of QeditText passed to the function if there is a way I can then it will work for what I want to do.

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


I need to be able to get the data from this, inside the worker function, is there a way?

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


edit: forgot the form.py here it is
http://pastebin.com/HksuSjkt

Answer

This is my Solution:

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

Output:

enter image description here

Comments