[PyQt] Deadlocking threads in PyQt 4.1.1?

Phil Thompson phil at riverbankcomputing.co.uk
Thu Mar 15 08:49:57 GMT 2007


On Wednesday 14 March 2007 9:34 pm, EXT-Stout, Hugh wrote:
> 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?

Some deadlock issues have been recent resolved - try current SIP and PyQt 
snapshots. You example works fine for me on Linux.

Phil


More information about the PyQt mailing list