[PyQt] Crash (segfault) when using PyQt4 Qthread

chris3110 chris31103110 at yahoo.fr
Mon Nov 17 23:13:09 GMT 2008


Hi David,

thanks a lot for your answer.  In fact this little program is my very first
attempt at a GUI application in Python, so it may very well look quite weird
to you :-)  Basically what I'm trying to do is very simple, I have a main
window that's waiting for input.  When you drag or paste a URL into it and
click "Ok", a separate thread is spawned and starts crawling the URL.  What
I want is the debug ouptut of this thread to go to a text box in the main
window.  Eventually there would be several worker threads, each with its own
output box in a tabbed interface for instance.

So this hopefully clarifies the intent of the piece of code I've submitted. 
I fully expect the naive approach I considered to be flawed, and I'd be more
than happy if somebody could quickly sketch the correct architecture for
this type of problems.

However besides that I still think that whatever garbage I feed it with,
Python should not give me a bare segfault.  This is expected in low-level
languages like C/C++ and perhaps Java but my understanding was that higher
level languages like Perl and Python were precisely aiming at providing an
abstraction layer on top of the operating system that would make this kind
of situation impossible.

Besides, the fact that nobody's seemingly able to reproduce the crash is
very annoying.  Isn't there a way to at least determine which library is
crashing, or even getting a stacktrace or something ?

Thanks for your help,
Christian



David Boddie wrote:
> 
> 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
> _______________________________________________
> 

-- 
View this message in context: http://www.nabble.com/Crash-%28segfault%29-when-using-PyQt4-Qthread-tp20255394p20550338.html
Sent from the PyQt mailing list archive at Nabble.com.



More information about the PyQt mailing list