[PyQt] Crash (segfault) when using PyQt4 Qthread

David Boddie david at boddie.org.uk
Sat Nov 15 01:48:47 GMT 2008


On Sat Nov 15 00:23:53 GMT 2008, chris3110 wrote:

> Also are you positive you can't reproduce this pb, even when clicking a lot
> in and out of the window, coming back and forth to it, etc ?  I find it
> hard to believe that this could be related to the python version.

I also tried to run it with Python 2.5.2, PyQt 4.4.3 and Qt 4.4.3 on
Ubuntu and couldn't make it crash.

Looking at your code, I'm concerned that you're accessing various logging
objects from two threads without any obvious safeguards.

In your main widget class, you do this:

        self.log = logging.getLogger('u.crawler')
        self.log.addHandler(WindowLogger(self.winlog))

So, all these objects are being created in the main GUI thread. Then you
create a thread, passing in a Page object:

    def accept(self):
        thread = CrawlerThread(Page())
        UrlCrawler.threadList.append(thread)
        thread.start()

The page object also gets a reference to the logging object:

class Page:
    def __init__(self):
        self.log = logging.getLogger('u.crawler')

In the thread, you access this object via the page object:

    def run(self):
        page = self.page
        page.log.info('Starting')
        page.analyze()

It seems that you have various objects that are being created in one thread
and used in another. Maybe that's OK if the main thread isn't trying to
access them at the same time as the worker thread.

One other point is that the logging handler you supply to the logging objects
accesses a GUI component:

class WindowLogger(logging.Handler):
    def __init__(self, window):
        self.window = window
        logging.Handler.__init__(self)
        
    def emit(self, record):
        self.window.appendPlainText(record.getMessage())

Since I presume this is being called from the worker thread, I would suspect
that this could be the source of your problems. Maybe you should emit a
signal here that you can receive in a slot in your UrlCrawler class.

I hope this helps. I'm sure that other people can correct me if I've
misidentified the cause of the problem.

David


More information about the PyQt mailing list