[Python-Dev] VM imaging based launch optimizations for CPython?

Nick Coghlan ncoghlan at gmail.com
Sat Dec 27 00:06:49 CET 2008

Mikko Ohtamaa wrote:
> Out of my head I know at least two particular module which could be
> refactored. I'd recommend as the best practice that everything should be
> imported lazily if it's possible.

We actually have a reason for discouraging lazy imports - using them
carelessly makes it much easier to accidentally deadlock yourself on the
import lock.

I agree that this contributes to the problem of long startup times though.

One sledgehammer approach to lazy imports is to modify the actual import
system to use lazy imports by default, rather than having to explicitly
enable them in a given module or for each particular import.

Mercurial does this quite nicely by overriding the __import__
implementation [1].

Perhaps PyS60 could install something similar in site.py? The trade-off
will be whether enough time is saved in avoiding "wasted" module loads
to make up for the extra time spent managing the bookkeeping for the
lazy imports.


[1] From a recent thread on Python-Ideas that Google found for me:

Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list