[mod_python] Using shared memory to do global persistence

Jack Diederich jack_diederich at email.com
Thu Jun 26 18:58:25 EST 2003

From: VanL <vlindberg at verio.net>
> I think that the nicest solution (in terms of API, at least) would be to 
> have an explicit namespace that is instantiated when mod_python is first 
> loaded.  Applications could then access this shared namespace by 
> importing it; anything declared in the namespace would be automatically 
> shared.
> For example.
> from mod_python import apache.shared as shared
> shared.myvar = 'something here'

In the future setting variables in other module spaces may be
outlawed in python.  Treating modules like classes/objects
is also strongly discouraged.

I think your use of dicts is dead on, something like:
def do_calc():
  cache = mod_python.shared
  if ('foo' not in cache):
    cache['foo'] = 'bar'
  return cache['foo']

As I mentioned during the long thread on what frameworks
should do, we could add two dicts to mod_python,
mod_python.page_cache # caching dict created per-request
mod_python.perm_cache # cache valid accross all processes for ever

perm_cache could actually lie and just keep the last X least
recently used entries, but the main idea is that it is long
lasting and valid accross all processes.  An intermediate
mod_python.proc_cache that is similarly 'permenent' for the
life of the process, but only caches values locally to the
current process might be useful in a pure-forking server
because it doesn't have to worry about locking.  If you are
threading you have to do locking anyway, so perm_cache is
more useful.


Sign-up for your own FREE Personalized E-mail at Mail.com

More information about the Mod_python mailing list