I have following python code. I would like to start new thread after the button is pressed because I would like to do something else with main thread. But code currently, I have does not seem to create a new thread when checked from pycharm concurrency diagram. I can only start new thread when I press the button. Also the program does not respond after pressing the button. Please help.
from PyQt4 import QtCore, QtGui
import sys
import subprocess
import re
import threading
sys.settrace
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class GuiMainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(420, 280)
self.centralwidget = QtGui.QWidget(MainWindow)
self.buttonTrans = QtGui.QPushButton(self.centralwidget)
self.buttonTrans.setGeometry(QtCore.QRect(50, 110, 131, 51))
self.buttonTrans.setObjectName(_fromUtf8("buttonTrans"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(70, 60, 281, 21))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
print(self)
def retranslateUi(self, MainWindow):
self.buttonTrans.setText(_translate("MainWindow", "Start", None))
self.connect(self.buttonTrans, QtCore.SIGNAL('clicked()'), self.setup_video)
def setup_video(self):
print("Setting up VIDEO")
t = threading.Thread(target=self.logging_thread()).start()
def logging_thread(self):
cmd33 = "ping www.google.com"
cmd3process = subprocess.Popen(cmd33.split(), stdout=subprocess.PIPE, shell=True)
import time
while True:
output3 = cmd3process.stdout.readline()
time.sleep(1)
if output3 == '' and cmd3process.poll() != None:
break
print(output3.decode("utf-8"))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = GuiMainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
-
How do you know it does not create a new thread?Mad Physicist– Mad Physicist2015年12月31日 02:26:59 +00:00Commented Dec 31, 2015 at 2:26
-
Because when I ran the code in pycharm concurrency, I can only see only main thread. I cannot see new thread after button is pressedMain– Main2015年12月31日 03:00:52 +00:00Commented Dec 31, 2015 at 3:00
-
Fair enough. Put that in your question and I will retract my close vote.Mad Physicist– Mad Physicist2015年12月31日 03:02:41 +00:00Commented Dec 31, 2015 at 3:02
-
Yes. The python program becomes not respondingMain– Main2015年12月31日 03:26:46 +00:00Commented Dec 31, 2015 at 3:26
-
What does it print? Does it print "Setting up VIDEO"?Tamas Hegedus– Tamas Hegedus2015年12月31日 15:35:35 +00:00Commented Dec 31, 2015 at 15:35
2 Answers 2
You invoke the method instead of passing it as a parameter to the new Thread here:
t = threading.Thread(target=self.logging_thread()).start()
Change it to:
t = threading.Thread(target=self.logging_thread).start()
2 Comments
Most likely, an exception is occurring in your thread. To identify and fix it try the following:
def logging_thread(self):
try:
cmd33 = "ping www.google.com"
cmd3process = subprocess.Popen(cmd33.split(), stdout=subprocess.PIPE, shell=True)
import time
while True:
output3 = cmd3process.stdout.readline()
time.sleep(1)
if output3 == '' and cmd3process.poll() != None:
break
print(output3.decode("utf-8"))
except:
import traceback
traceback.print_exc
When you have identified the cause of the error, you have two options:
- If the error is something you did (e.g. a mistyped attribute), remove the
try...exceptblock entirely. - If there is a condition in your code that may legitimately occur, retain the block but make the exception type-specific. For example
except ValueError:. Also, improve the actual handler in that case.