[mod_python] shared memory and module import problems

Marc D. Murray marcdm at phronein.com
Tue Feb 21 10:11:23 EST 2006

If u can, you might want to have a look at memcached

It's pretty fast, it's an external process to Apache and there's a
simple python client available.

I use it to cache rendered pages then my handler checks for the page
there before trying to re-generate it.


/Marc DM

On Tue, 2006-02-21 at 06:22 -0800, Kevin J. Smith wrote:
> Hi,
> I have a page controller that all handlers import with "from
> myapp.gui.pagecontroller import PageController".  Each handler is a
> class derived from the PageController.
> In the page controller, I have attempted to set up a shared memory
> cache for all the controllers with code very similar to the
> following: 
> #...
> import threading
> CACHE_LOCK = threading.Lock()
> cache = {}
> def _fillCache():
>     cache['one'] = '1'
>     cache['two'] = '2'
> class PageController:
> # ...
>     def __init__(self): 
>         #....
>         if not len(cache.keys()):
>             CACHE_LOCK.acquire()
>             _fillCache()
>             CACHE_LOCK.release()
>         #...
> However, I have discovered that this module is continually reimported
> because it ALWAYS calls the _fillCache() function despite all the
> handlers running within the same interpreter. 
> I have upgraded to mod_python 3.2.7 noticing that in the change log
> there is a lot about fixing module import problems but my app is still
> behaving the same way.
> The flow of my app is that all requests are handled by a
> frontController handler (this is explicitly set in the httpd config)
> which, after checking the session and authentication and what-not and
> based on the request path, forwards the request onto one of my
> pageController handlers via util.redirect().  If I have followed
> Graham's notes about module importing correctly, the frontController
> and pageController classes would be imported via import_module()
> whereas the actual pageController module is imported via the regular
> python import mechanism. 
> I have spent some time reading about the various "uniquenesses" of
> module importing within mod_python but it hurt my head slightly and I
> didn't know if most of it reflected the state of affairs in version
> 3.2.7.  From reading Graham's page I don't believe I should have a
> problem with the PageController handler continually being reimported
> but it certainly appears to be doing just that.
> Can someone shed some light on what I may be doing wrong or a possible
> solution to setting up a shared cache within an interpreter?  I know
> that if I run similar code outside of mod_python my cache design works
> fine. 
> Cheers
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list