[mod_python] Problems inheriting BaseSession

Byron Ellacott bje at apnic.net
Mon Aug 30 10:41:09 EDT 2004


R. Pelizzi wrote:
> A few days ago i was trying to implement a "counter" for a site... i had 
> to keep track of the number of active sessions and i was asking for a 
> way to store variables global to the whole server. There was no way to 
> do that, so i tought i could implement a dbm db file and pickle the 
> values. I can't see any way to implement an efficient locking mechanism 
> (since semaphores cannot be shared between interpreters), what would you 
> use? Would using files as semaphores be very inefficient?
> Anyway, i thought i would just implement locking later, and i started 
> the implementation. I created a class called Application, which is 
> perfectly working (tough not thread-safe as i explained before) and can 
> be access like a dictionary.

I suggest you look at Session.py.  It demonstrates how to use the Apache 
APIs to create global locks and how to set cleanup handlers. 
Interestingly, there is only one reason you couldn't use 
Session.Session() directly: you can't create a session with a 
predetermined session ID.  If you create that out of band, or alter 
Session.py to allow a fixed sid or create a session with a given sid, 
you could just use:

app_obj = Session.Session(req, 'MyGlobalSID')

And there's your dbm or memory backed global session.  Note that if it's 
a memory session you'll lose your data each time the server restarts; if 
that's a problem you should create a DbmSession directly instead.

Updating the Session module to allow a constant sid would perhaps 
involve altering the base __init__() to have a new optional argument of 
"fixed=0."  Then add checks to the bits in __init__ dealing with session 
IDs, to neither check nor create cookies, and not create a new sid if 
the session couldn't be loaded.

-- 
bje


More information about the Mod_python mailing list