Reputation: 67
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
Upvotes: 2
Views: 1138
Reputation: 243897
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:
Upvotes: 1
Reputation: 2639
Im not an expert on PyQt but I assume you keep an entry widget in your form.py
eyllanesc mentioned and I can see you imported.
One of the reasons you use special variables to contain string and such from UIs, is that the class it resides in is occupying a designated memory space (good when it comes to scopes and such).
When it comes to multiPROCESSING the memory spaces is no longer shared and you cannot reach this variable any longer. If you however use multiTHREADING, it is a new thread, but in the same process, which means it can access the Qt string you want.
The syntax is for this purpose the same
import threading
def foo(): pass
t = threading.Thread(target=foo,
args=[])
t.daemon = True
t.start()
Learn more about threads here
Upvotes: 0