0

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_())
asked Dec 31, 2015 at 2:23
5
  • How do you know it does not create a new thread? Commented 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 pressed Commented Dec 31, 2015 at 3:00
  • Fair enough. Put that in your question and I will retract my close vote. Commented Dec 31, 2015 at 3:02
  • Yes. The python program becomes not responding Commented Dec 31, 2015 at 3:26
  • What does it print? Does it print "Setting up VIDEO"? Commented Dec 31, 2015 at 15:35

2 Answers 2

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()
answered Dec 31, 2015 at 3:51
Sign up to request clarification or add additional context in comments.

2 Comments

@Main This is definitely a problem. Combine that with my recommendation and see if anything happens.
Yes. This has been a problem.
1

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:

  1. If the error is something you did (e.g. a mistyped attribute), remove the try...except block entirely.
  2. 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.
answered Dec 31, 2015 at 3:08

1 Comment

I added try-except blocks as you suggested. But still there is no console exceptions I could see. The program still goes unresponsive. Please help

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.