High CPU usage and other bits and bobs

  • mond


    Hope you had a good holiday!

    I tried out the hello world example from CVS but running it without actually engaging in the app gives me a CPU usage of around 85% (the reason I found this out was my laptop fan kicked in!).

    Upon closer inspection of the code and some profiling it seems that the main culprit is the "present" method of the Presenter class because it clears the screen and redraws everything even though nothing may have changed. Would you agree? (Since you know the code base a lot better than me.)

    I think it would be better to adopt the same principles as the RenderUpdates class in pygame. (see here: http://www.pygame.org/docs/ref/pygame_sprite.html#RenderUpdates\) The idea is that you only update what actually is changed on the screen. This could be done through the use of a dirty flag, which seems incomplete in the implementation.

    While I was investigating the above problem I came across some bits that needed changed (listed below). (Remnants of the old code base no doubt!)

    How would I go about submitting a patch? (At the moment I have the downloadable pyui2 installed as a site package and I have checked out the module from CVS. Could you tell me how I could easily make a change to the CVS code and run it to see what happens? Would I have to update the import path list to include the directory of the CVS code instead of using the site package?)

    Bits that need changed/looked at:
    1. In the "quit" method of the Desktop class I think there is a spelling mistake as there is "self.window = []" where I think it should be "self.windows = []".

    2. Shouldn't the Window class be a widget too? (And thus be in the widgets subfolder)

    3. In Window's constructor shouldn't the code that creates the Window add it to the desktop rather that the Window doing it for you (I suppose this is just a matter of consistency since you can't have windows inside windows, i.e. a Window will always be added to the Desktop).

    4. In the "setDirty" method of Window:

        def setDirty(self, collide = 1):
            self.dirty = 1
            if self.dirty:
            if collide:

    The test "if self.dirty:" will always be true since it was just set so the function will always return and not execute the other code. What's going on here? ;)

    5. In the "present" method of the Presenter class, the code that iterates over the windows is quite unpythonic:

                windowIndex = len(windows)-1
                while windowIndex >= 0:
                    window = windows[windowIndex]
                    if window.show:
                        if window.dirty or self.mustFill == 1:


                    windowIndex = windowIndex - 1

    Would this be better?

            for window in windows.reverse():
           if window.show:
              ... etc ...

    • Bluecat


      Thank you. I did have a very enjoyable holiday. We drove nearly 9300 miles across the US, with a visit to Canada thrown in for good measure. It was the trip of a lifetime.

      I do agree with your assessment of the Presenter class, and my intention is to make it far more efficient, and redraw only what is necessary to be redrawn. It's something that I will endeavor to get working either in release 0.3 or 0.4, though it's probably better to do sooner rather than later.

      Also thanks for the list. I've addressed items 1, 4, and 5. The setDirty issue was a short circuit, though I'm not sure why. I've removed it, but the called functions still need to be implemented.

      Issue 2, I do agree, but it won't be in 0.2.1 (which I hope to release this weekend if I get time). I do remember looking at this when I first started developing PyUI2, and I think there was a reason I didn't make the Window class a widget at the time, but I can't remember it offhand. When I start working on this, it will probably come to me.

      Issue 3. I'll have to have a look into this. I can't give you an answer at the moment.

      For submitting patches, the best way to do this is to use the patch link on the PyUI2 project page. It allows you to enter a detailed description of the patch and attach the files that have changed. I'll then look at the patch and either merge it into the code, or implement it another way.

      With the CVS issue, I've been developing right out of the site-packages PyUI2 lib so I haven't had your issue. I suspect you could set the PYTHONPATH variable to look at your CVS workspace instead of the site-package directory. It might be worth asking around on the Python forums/lists to see how other developers do this.

      Anyway, I hope this helps.