[PyQt] Freezes and crashes with signal autoconnection

Phil Thompson phil at riverbankcomputing.com
Mon Oct 5 12:51:39 BST 2009


On Mon, 5 Oct 2009 02:24:09 -0700 (PDT), Christian Roche
<christian.roche.fr at gmail.com> wrote:
> Hi there,
> 
> I'm having some problems with a fairly simple multi-threaded application
> under PyQt 4.5.4 and Windows XP. I have a number of worker threads that
> feed
> one consumer in the main GUI thread. Initially I had the worker threads
> directly call the "add" method of the consumer object; this piece of code
> was protected by a mutex. So in short:
> 
> class Worker(QThread):
>  def run(self):
>    ...
>    cache.add(link)
> 
> class Cache():
>  def add(self, link):
>    mutex.lock()
>    # Add link to queue
>    mutex.unlock()
> 
> cache = Cache()
> 
> This works pretty well but still I'm not completely satisfied. It's bad
to
> execute code that doesn't belong to your thread, right? So I tried to
> replace that with a signal/slot mechanism:
> 
> class Worker(QThread):
>  addLink = pyqtSignal(Link)
> 
> def init(self):
>   self.addLink.connect(cache.add)
> 
> def run(self):
>    ...
>   link = Link(blah blah)
>   self.addLink.emit(link)
> 
> class Cache(QObject):
>  def add(self, link):
>    # Add link to queue
>    # No more mutex since access is now serial, right ?
>    ...
> 
> However the application freezes after a while. No more GUI activity,
window
> not even repainting anymore and Windows tells me "this application is not
> responding etc" when trying to close. This doesn't happen if I use
> Qt.DirectConnection or Qt.BlockingQueuedConnection it seems, although I
> have
> no idea why.

This rings a bell - I think with something I replied on the mailing list
not too long ago. Something about Qt seemingly not doing the right thing
when AutoConnection is used. But I can't remember the details (or find the
message).

> I also notice that with Qt.AutoConnection, the application crashes with
no
> error message unless I use 'PyQt_PyObject' types, with the quotes, in all
> signal definitions (instead of Link for instance); I don't have this
> problem
> either when using Qt.DirectConnection or Qt.BlockingQueuedConnection.
> 
> So does anyone understand what's going on here under the hood? Is the
> signal/slot approach the correct way to garantee serial access to a
shared
> resource between threads (the link queue in this example)? Why is this
> whole
> thing crashing and freezing like that?

Can you try with the latest installer (though it does contain a nasty
thread-related regression).

Do you have a test case?

Phil
> Chris


More information about the PyQt mailing list