Re: Importing Cheetah.Template.Template unusually slow?

Graham Dennis <[email protected]> Thu, 28 Jan 2010 11:58:20 +1100
Newsgroups gmane.comp.python.cheetah
Message-ID <[email protected]>
I tried to post this message earlier, but I think my mail server is having a hissy fit.  Apologies if anyone gets this twice.

On 28/01/2010, at 8:19 AM, Matthew Beauregard wrote:
> So I guess I'd have to blame some interaction between Cheetah and Apple's framework python.  I'm not sure what to do next.

I remember having this problem myself and I traced it down to Servlet.py where it tries to do:
from WebKit.Servlet import Servlet as BaseServlet.

What's happening here is that on Mac OS X Leopard and later the 'WebKit' import resolves to the system framework WebKit which is available via the PyObjC bridge. However, once the import of 'WebKit' has succeeded, 'WebKit.Servlet' fails, and you're wasting much of your time on a line that does nothing.

I created a hack to get around this which installs an empty module called 'WebKit' (only on Mac OS X), so that when Cheetah tries to import WebKit it gets this module instead and fails much faster.

def leopardWebKitHack():
    """
    Hack for Mac OS X Leopard and above so that it doesn't import
    the web rendering framework WebKit when Cheetah tries to import
    the Python web application framework WebKit.
    """
    if sys.platform =3D=3D 'darwin' and not 'WebKit' in sys.modules:
        module =3D type(sys)
        sys.modules['WebKit'] =3D module('WebKit')

Just make sure you call that before importing Cheetah.Template.

time python -c 'from Cheetah.Template import Template'

real	0m0.682s
user	0m0.550s
sys	0m0.121s

time python -c 'import sys; sys.modules["WebKit"] = type(sys)("WebKit");from Cheetah.Template import Template'

real	0m0.130s
user	0m0.083s
sys	0m0.044s


Cheers,
Graham


------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com