[mod_python] Session Timeouts

Daniel J. Popowich dpopowich at comcast.net
Mon Dec 12 15:06:49 EST 2005


Peter Sanchez writes:
> I have a question regarding session timeouts. I would like to set the  
> timeout, to say, 1 hour. Instead of whatever the default is now. I  
> know of set_timeout() function, but my question is this. Can I use  
> the set_timeout() multiple times? For example, a user comes to my  
> site, logs in, I set the timeout to 1 hour. Every page in the members  
> section checks the session, and every time a page is accessed, I want  
> to reset the time to 1 hour from the time that page was accessed. So  
> will using set_timeout() every time do that?
> 
> I hope that makes sense, but it probably doesn't ;) So here is a  
> "flow chart"
> 
> 10:45 - User logs in, set_timeout() to 1 hour from now (11:45)
> 10:47 - User accesses email page, set_timeout to 1 hour from now (11:47)
> 
> etc.. on and on it goes.
> 

You don't have to do anything special to get what you want...it works
that way out-of-the-box.  If you create your session specifying a
timeout of one hour:

   ses = Session(req, timeout=3600)
   ...
   ses.save()

then the session will stay alive as long as it's *accessed* within an
hour and when it is, the access time will be reset so you have another
hour, and so on...

The session code does something like this:

  if time.time() - ses.last_accessed() > ses.timeout():
     SESSION-HAS-TIMED-OUT

The last accessed time of a session is updated to the current time
with every request (when the session is instantiated), but not stored
until you call save() (if you never call save() then you will never
re-use a session).

Cheers,

Daniel Popowich
---------------
http://home.comcast.net/~d.popowich/mpservlets/



More information about the Mod_python mailing list