[PyQt] PyQt4 and threading

Jason H scorp1us at yahoo.com
Wed Oct 14 21:21:51 BST 2009

GUI operations can only be done in the main thread.

----- Original Message ----
From: Ole Streicher <ole-usenet-spam at gmx.net>
To: pyqt at riverbankcomputing.com
Sent: Wed, October 14, 2009 9:26:25 AM
Subject: [PyQt] PyQt4 and threading


I have a problem when I use multi threading with pyqt4: every of my
widgets has a worker thread that updates the content on changes. The
worker thread is build around Python threading (error handling removed
to shorten the code):

class AsyncThread(threading.Thread):
    def __init__(self, func):
        self._cond = threading.Condition()
        self._func = func
        self.scheduled = False

    def run(self):
        while True:
            with self._cond:
                while not self.scheduled:
                self.scheduled = False

    def __call__(self):
        with self._cond:
            self.scheduled = True

A call of the AsyncThread object will trigger a call of the enclosed
function func(). I use this to update a large QGraphicsView:

def update_map():
    # ...
    for item in scene.items: # 'items' contains some 1000s QGraphicsRectItems

async_update_map = AsyncThread(update_map)

QtCore.connect(..., ..., async_update_map)

The "connect" connects, for example, cursor changes with the update.

The problem is now that I get random *crashes* with that code. This
ranges from a simple "segmentation fault" to hundreds of lines glibc
Backtraces like:

*** glibc detected *** python: realloc(): invalid pointer: 0x00007ffcfa2c5360 **
======= Backtrace: =========

Beside "invalid pointer", I also get "double free or corruption
(fasttop)" and similar errors.

My question is now whether this is a Bug of PyQt4 or if I use the
Threading incorrectly? Do I have to use Qt Threads instead? And how do I
do locking (the threading.Condition) in that case?



PyQt mailing list    PyQt at riverbankcomputing.com


More information about the PyQt mailing list