[Python-Dev] Dynamic module namspaces

Johan Dahlin jdahlin at async.com.br
Mon Jul 17 15:41:30 CEST 2006


Andrew Bennetts wrote:
> On Sat, Jul 15, 2006 at 03:38:04PM -0300, Johan Dahlin wrote:
>> In an effort to reduce the memory usage used by GTK+ applications 
>> written in python I've recently added a feature that allows attributes 
>> to be lazy loaded in a module namespace. The gtk python module contains 
>> quite a few attributes (around 850) of which many are classes or 
>> interfaces (150+)
> 
> Have you seen the "demandload" hack that Mercurial uses?  You can find it here:
>     http://selenic.com/repo/hg?f=cb4715847a81;file=mercurial/demandload.py

It seems quite similar to Philips Importer, it's not completely solving the
problem I'm having, since I do something like this:

class LazyNamespace(ModuleType):
    def __init__(self, realmodule, locals):
        attributes = {}
        for attr in realmodule._get_lazy_attribute_names():
            attributes[attr] = None

        def __getattribute__(_, name):
            if name in attributes:
                value = realmodule._construct_lazy_attribute(name)
	    ...
            return value

There are almost 1000 symbols in the gtk namespace, creating all of them at
import time wastes memory and speed, while I've been mainly looking at the
memory consumption speed will also benefit. Importing gtk at this point
quite fast actually, less than 200ms on my fairly old box.

GUI programs does not need to be as responsive as command line applications
as hg & bzr, users seems to accept that it takes a second or a few to start
up a GUI application.

-- 
Johan Dahlin <jdahlin at async.com.br>
Async Open Source


More information about the Python-Dev mailing list