[PyQt] Why does QApplication.processEvents() increase performance?

Andreas Pakulat apaku at gmx.de
Wed Apr 29 00:46:02 BST 2009


On 28.04.09 15:18:22, Brent Villalobos wrote:
> I have been conducting some performance tests on PyQt4 and I've run into  
> a condition that is very counter-intuitive to me.  I am looping over a  
> list of QPushButton widgets (4900 of them) and calling their hide() and  
> show() methods.  When just calling those two methods, my application  
> loops over all of them in about 60 seconds.  However, if I insert an  
> "app.processEvents()" call in that loop, I get much better times around  
> 30 seconds.  Why is adding that processEvents method in the loop causing  
> such a dramatic improvement in performance? 

I suggest to read the processEvents source code and that of hide and
show. My guess is that there's some compression of events happening or
something like that.

> I know this isn't the most  
> realistic example, but I'm trying to show some people the importance of  
> app.processEvents() to not lock up the GUI when doing large operations.   
> I would like to have an explanation for the performance improvment.

Please don't do that, you shouldn't use processEvents() unless you know
very very very well what you do and how Qt works internally. There are
far better ways to let the event loop run while you're doing a bunch of
work, even with Python and the GIL. You can easily get yourself into
hard to track bugs with processEvents, because objects are being deleted
when you expect them to be around still, or get user input at a point
that wasn't expected, all kinds of races. 

There are very few cases where its not possible to split up the work and
use timers or move it into a separate thread.

Andreas

-- 
Your step will soil many countries.


More information about the PyQt mailing list