PyQt:QThread:线程仍在运行时被破坏

时间:2018-01-13 23:27:59

标签: python multithreading pyqt qthread

尝试了所有相关的解决方案,在这个QThread问题上找不到,只是无法解决它,如果我运行以下线程我遇到以下警告/错误的崩溃:

QThread::wait: Thread tried to wait on itself
QThread: Destroyed while thread is still running

有时这会在一分钟后发生,有时需要半天才会发生。 这是代码:

from PyQt4 import QtGui, QtCore
import socket
import time


class WorkThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        try:
            MyApp.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
            MyApp.udp_socket.connect((SERVER_IP, SERVER_PORT))
            MyApp.udp_socket.settimeout(2)
            MyApp.udp_socket.send(UDP_PACKAGE)
            # Receive Response 1
            udp_response = MyApp.udp_socket.recvfrom(1024)
            time.sleep(0.2)
            MyApp.udp_socket.close()
            result_returned = str(udp_response)
        except Exception:
            if MyApp.udp_socket:
                MyApp.udp_socket.close()
            result_returned = 'error'
        time.sleep(0.3)
        self.emit(QtCore.SIGNAL('done(QString)'), result_returned)


class MyApp(QtGui.QTabWidget, FUNC_ui.Ui_FUNC):
    def __init__(self):
        QtGui.QTabWidget.__init__(self)
        FUNC_ui.Ui_FUNC.__init__(self)
        self.setupUi(self)

    def finished(self, result):
        if result == 'error':
            pass
        else:
            # do stuff
            pass
        self.store_thread = WorkThread()
        self.connect(self.store_thread, QtCore.SIGNAL("done(QString)"), self.finished)
        self.store_thread.start()
        return

我在一个按钮上启动线程,然后像上面的代码中看到的一遍又一遍地调用它。 据我所知,这个线程不应该收集垃圾,那么问题出在哪里?

编辑: 还尝试使用数组来存储线程并直接使用udp_socket,但没有成功:

class WorkThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        self.wait()

    def run(self):
        try:
            udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
            udp_socket.connect((SERVER_IP, SERVER_PORT))
            udp_socket.settimeout(2)
            udp_socket.send(UDP_PACKAGE)
            udp_response = udp_socket.recvfrom(1024)
            time.sleep(0.2)
            udp_socket.close()
            result_returned = str(udp_response)
        except Exception:
            if udp_socket:
                udp_socket.close()
            result_returned = 'error'
        time.sleep(0.3)
        self.emit(QtCore.SIGNAL('done(QString)'), result_returned)


class MyApp(QtGui.QTabWidget, FUNC_ui.Ui_FUNC):
    def __init__(self):
        QtGui.QTabWidget.__init__(self)
        FUNC_ui.Ui_FUNC.__init__(self)
        self.setupUi(self)
        self.threadPool = []

    def finished(self, result):
        if result == 'error':
            pass
        else:
            # do stuff using 'result'
            pass       
        self.threadPool.append(WorkThread())
        self.connect(self.threadPool[len(self.threadPool)-1], QtCore.SIGNAL("done(QString)"), self.finished)
        self.threadPool[len(self.threadPool)-1].start()
        return

编辑:我正在使用的最终代码,有时线程会在运行时被破坏:

class WorkThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)

    def run(self):
        try:
            udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
            udp_socket.connect((SERVER_IP, SERVER_PORT))
            udp_socket.settimeout(2)
            udp_socket.send(UDP_PACKAGE)
            udp_response = udp_socket.recvfrom(1024)
            time.sleep(0.2)
            udp_socket.close()
            MyApp.result_returned = str(udp_response)
        except Exception:
            MyApp.result_returned = 'error'
        time.sleep(0.3)


class MyApp(QtGui.QTabWidget, FUNC_ui.Ui_FUNC):
    def __init__(self):
        QtGui.QTabWidget.__init__(self)
        FUNC_ui.Ui_FUNC.__init__(self)
        self.setupUi(self)
        self.threadPool = []

    def finished(self):
        if MyApp.result_returned == 'error':
            print('ERROR')
        else:
            print(MyApp.result_returned)
        self.threadPool.append(WorkThread())
        self.threadPool[len(self.threadPool)-1].finished.connect(self.finished)
        self.threadPool[len(self.threadPool)-1].start()
        return

0 个答案:

没有答案