Main
Main

Reputation: 1842

Cannot start new python thread

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

Upvotes: 0

Views: 120

Answers (2)

Tamas Hegedus
Tamas Hegedus

Reputation: 29896

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

Upvotes: 2

Mad Physicist
Mad Physicist

Reputation: 114230

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.

Upvotes: 1

Related Questions