[mod_python] [mod python] ABout Sessions

Jim Gallacher jpg at jgassociates.ca
Sun Jun 4 10:01:55 EDT 2006

S.R.Pardá wrote:
> Hi, 
> Sorry to ask again.
> I need some acclarations about the session object.
> I have been developing VBScripts ASP pages from 1999. I have used a lot
> the ASP's session and application objects. Now I'm decided to use PSP
> Mod_Python instead.
> I'm using Ubuntu, with Apache2 and Mod_Python for server and the Firefox
> web client.
> 1st. Question :
> Only One Session is created for various Firefox instances in the same
> PC.
> In ASP I could open 2 windows with different program views and data, on
> different sessions (opening a new explorer. If you select New in menu
> Archive with a page opened, you will have a another new page with same
> session that the previous page)
> Is possible to change how session works to make that ?
> If not, can I make only one page could be open to reduce the possibility
> of error (I Think in use target="APPL-WINDOW-NAME" in all the links,
> that make IE open page in an explicit named window )

Not directly, but you could likely subclass one of the session classes 
to do what you want.

> 2nd. Question:
> Usually, in ASP is possible to create a Session referencing a DB
> Connection object. So you can use that object to access the server
> during the session using that stored connection. In IIS ASP that is fast
> and secure.
> I have been reading about problems storing not sys objects in sessions,
> so I am unsure about that.

You are correct. The session data is a python pickle (except for 
MemorySession on Windows) and you will have problems if you try to 
pickle the db connection object.

> 3nd. Question:
> All the sessions in IIS ASP would have the connection object (yes that I
> mencioned in 2nd. point) previously stored because it was stored on a
> Session_OnStart() event generated when a page uses Session object and no
> Session is active for that petition, defined in global.asa web
> configuration,  
> So in the pages you can use :
> 	connectionForThePage = Session('connectionForTheSession')
> and OLE! you will have a connection created , because if session not
> exist when page load Event will be fired and my code inserted into
> on_star will create an object and let it stored in session before the
> page executed (so Sesssion('con...') always exist)
> Is there a Session_OnStart equivalent in Mod_Python (remember I am using
> PSP , I think Publisher can permit write my handler and check that , but
> I wanted a transparent method not related to write a handler if it is
> possible)

As I understand your question I would say the answer is no. The best you 
can do is use sess.is_new() to test if it is an existing session. If 
not, you could then do your initialization.

> 4nd. Question:
> Because Apache is threaded, varios modules are opened by various
> Mod_python server instances. So the same modules can be loaded more than
> one time. So global variables of a module are different to each session,
> and to make it hard  possibly different request on same session.
> In IIS ASP I could  use application object to store data to be accesed
> into different sessions in all the sessions of the server. And
> Application object could be LOCKED and UNLOCKED to ensure thread
> security.  
> In Mod_Python, the only solution its to write a class, that servers
> global variables from a Database with appropiate locking ?

As you have surmised, global variables are a problem when using apache. 
The easiest solution is to store you data in a session if possible, as 
locking is turned on by default. You can turn it off, as well as lock 
and unlock the session manually if required but generally you don't need 
to worry.


More information about the Mod_python mailing list