[PyQt] isRunning() returns true after QThread completion

Lars Beiderbecke lars.beiderbecke at googlemail.com
Sun Feb 26 14:55:25 GMT 2012


Hello,

In my application some QThreads still return isRunning() == true when
they should be completed.

More specifically, I'm using QThreads to load images asynchronously in
the background.  By emitting a signal, the thread notifies the main
window that the pixmap has been loaded and the corresponding widget
can be updated (code has been simplified):

class ImgRequest(QtCore.QThread):
 def run(self):
     # ... load image ...
     self.emit(QtCore.SIGNAL("sigDone"), self)

class MainWindow(QtGui.QMainWindow):
 self.requests = set()

 def request(self, filename):
     t = ImgRequest(self, filename)
     self.connect(t, QtCore.SIGNAL("sigDone"), self.ack)
     self.requests.add(t)
     t.start()

 def ack(self, t):
     # ... update image ...
     if t.isRunning():
         print "WARNING: thread still running"
         t.wait()
     self.requests.remove(t)

When I run above code, however, I'll often get WARNING messages, i.e.,
QThread.isRunning() is returning true even though its corresponding
run() method has been completed.

Is this merely a race condition between the signal and the actual
completion of run(), or am I missing something fundamental about
QThreads?  Do I really need isRunning() and wait()?  And finally, is
there a better way to deal with the QThread objects than storing them
in a set so that the GC won't kill them while running?

Regards
Lars


More information about the PyQt mailing list