无法启动新的python线程

时间:2015-12-31 02:23:54

标签: python multithreading

我有以下python代码。按下按钮后我想开始新的线程,因为我想用主线做其他事情。但是代码目前,我从pycharm并发图中检查时似乎没有创建新线程。我按下按钮时只能启动新线程。按下按钮后程序也不响应。请帮忙。

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

2 个答案:

答案 0 :(得分:2)

您调用该方法而不是将其作为参数传递给新的Thread

t = threading.Thread(target=self.logging_thread()).start()

将其更改为:

t = threading.Thread(target=self.logging_thread).start()

答案 1 :(得分:1)

最有可能的是,您的帖子中发生了异常。要识别并修复它,请尝试以下操作:

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

确定错误原因后,您有两种选择:

  1. 如果您遇到错误(例如输入错误的属性),请完全删除try...except块。
  2. 如果代码中存在可能合法发生的情况,请保留该块,但要使特定类型的异常。例如except ValueError:。此外,在这种情况下改进实际的处理程序。