[PyQt] safely closing QThread when application exits.

Yaşar Arabacı yasar11732 at gmail.com
Tue Jun 28 00:44:10 BST 2011


hi,

I am pyqt beginner and followed tutorials on the internet to learn it until
recently. Then I have wanted to develop my own application. I am developing
a simple chat application.

I want to have 2 windows running independently (in other words two separate
applications), one for server and one for client. When server starts to run,
it is going to wait for client to run, and they will chat. So far, I could
only start doing server side. I am using QThread, which is a subject rather
unclear to me. I am getting this error when I close my application from X
button:

QThread: Destroyed while thread is still running

I am guessing that this occurs because I am not closing the thread safely
before application exists. I was wondering how I can make my thread respond
to application exist so that it can close itself. Here is my code:

====
chat_server.py
====

import sys
from PyQt4 import QtCore,QtGui
from chat_ui import Ui_MainWindow
import socket

class Server(QtGui.QMainWindow):

    def __init__(self,parent=None):
        super(Server,self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.textEdit.append("Waiting for inbound connections")
        self.netconnector = NetConnectWorker(self)
        self.netconnector.attemptConnect()


self.connect(self.netconnector,QtCore.SIGNAL("Connected()"),self.connected)

self.connect(self.netconnector,QtCore.SIGNAL("dataRecieved(QString)"),self.messageRecieved)

self.connect(self.netconnector,QtCore.SIGNAL("socketError(QString)"),self.socketError)

    def connected(self):
        self.ui.textEdit.append("Connected")

    def messageRecieved(self,data):
        self.ui.textEdit.append(data)

    def socketError(self,msg):
        print msg
        self.ui.textEdit.append("Encountered a socket error:\n %s" % msg)


class NetConnectWorker(QtCore.QThread):

    def __init__(self,parent=None):
        super(NetConnectWorker,self).__init__(parent)
        self.exiting = False
        self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.host = ''
        self.port = 50000
        self.backlog = 5
        self.address = ""
        self.client = ""

    def __del__(self):
        self.exiting = True
        self.wait()
        if self.client:
            self.client.close()

    def attemptConnect(self):
        self.start()

    def run(self):
        try:
            self.socket.bind((self.host,self.port))
        except socket.error,error:
            print dir(error)
            self.emit(QtCore.SIGNAL("socketError(QString)"),error.message)
            self.exit()
        self.socket.listen(self.backlog)

        self.client , self.address = self.socket.accept()

        if self.client:
            self.emit(QtCore.SIGNAL("Connected()"))

        while not self.exiting:
            data = self.client.recv(1024)
            if data:
                self.emit(QtCore.SIGNAL("dataRecieved(QString)"),data)



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    ex=Server()
    ex.show()
    sys.exit(app.exec_())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110628/44f7abe7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chat_server.py
Type: text/x-python
Size: 2413 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110628/44f7abe7/attachment-0002.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chat_ui.py
Type: text/x-python
Size: 1527 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110628/44f7abe7/attachment-0003.py>


More information about the PyQt mailing list