[PyQt] Deadlocking threads in PyQt 4.1.1?

EXT-Stout, Hugh Hugh.Stout at boeing.com
Wed Mar 14 21:34:09 GMT 2007


Hello,

I'm using PyQt 4.1.1 compiled against Qt 4.2.2. on WinXP with Python
2.5. I have been having a strange issue that appears to be some sort of
deadlock when using QThreads.

Below is some sample code that reproduces the problem for me.


----- CODE -----
import PyQt4.Qt as qt

class CounterWidget(qt.QWidget):
    def __init__(self):
        qt.QWidget.__init__(self)
        self.count = 0
        
        self.layout = qt.QHBoxLayout(self)
        
        self.label = qt.QLabel()
        self.label.setText("COUNT: %d" % self.count)       
        
        self.layout.addWidget(self.label)
        
        self.thread = CounterThread()
        self.thread.connect(self, qt.SIGNAL('START'),
                            self.thread.work_loop)
        self.connect(self.thread, qt.SIGNAL('INCR_COUNT'),
                     self.incr_count, qt.Qt.QueuedConnection)
        self.connect(self.thread, qt.SIGNAL('finished()'),
                     self.thread, qt.SLOT('deleteLater()'))

        self.thread.start()
        
    def incr_count(self):
        self.count += 1
        self.label.setText("COUNT: %d" % self.count)
        
    def thread_start_work(self):
        self.emit(qt.SIGNAL('START'))
    
        
        
class CounterThread(qt.QThread):
    def __init__(self):
        qt.QThread.__init__(self)

    def run(self):
        qt.QTimer.singleShot(0, self.work_loop)
        self.exec_()
        
    def work_loop(self):
        for x in xrange(500):
            self.emit(qt.SIGNAL('INCR_COUNT'))
            self.msleep(1)
        
if __name__ == "__main__":
    app = qt.QApplication([])
    w = CounterWidget()
    w.show()
    app.connect(app, qt.SIGNAL('lastWindowClosed()'),
                app, qt.SLOT('quit()'))
    app.exec_()

----- END CODE -----

The code above will make a simple textlabel widget and attempt to update
an integer counter via signals. This code usually locks up around count
#430-450 for me. The GUI window freezes and the app has to be killed
forcefully.

Any idea why this is happening?


Hugh "Trey" Stout



More information about the PyQt mailing list