[PyQt] safely closing QThread when application exits.

Nathan Weston nathan at genarts.com
Thu Jun 30 17:05:53 BST 2011


It looks like you're relying on the _del_ method of your worker thread 
to shut it down. I'm not sure if that's a good idea -- Python doesn't 
provide very strong guarantees about when objects will be destroyed.

You might try shutting down your worker (and waiting on it) from the 
main thread instead -- maybe in your window closeEvent.

On 6/27/2011 7:44 PM, Yaşar Arabacı wrote:
> 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_())
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt

-- 
. . . . . . . . . . . . . . . . . . . . . . . . .
Nathan Weston                   nathan at genarts.com
GenArts, Inc.                   Tel: 617-492-2888
955 Mass. Ave                   Fax: 617-492-2852
Cambridge, MA 02139 USA         www.genarts.com


More information about the PyQt mailing list