have had similar thoughts about sessions. Sessions are all or
nothing even if you only want them some of the time. However, instead
of complicating the session interface I think it would be much cleaner
to have web.py defer loading sessions until a session variable is
accessed. I cracked open session.py and with a few minor tweaks I was
able to get the simple cookbook example
http://webpy.org/cookbook/sessions working running the built in web
server. This was just a quick hack so there may be some glaring
oversight to why this is a bad idea that I have missed.
Basically, Instead of loading sessions in processor(), the sessions
are loaded the first time __getattr__ is called.
file:sessions.py
------------------------------------
def __getattr__(self, key):
self._lazy_load()
return utils.ThreadedDict.__getattr__(self, key)
def _processor(self, handler):
"""Application processor to setup session for every request"""
#self._cleanup()
#self._load()
try:
return handler()
finally:
if self._session_loaded():
self._save()
def _session_loaded(self):
try:
utils.ThreadedDict.__getattr__(self, "session_id")
return True
except AttributeError:
return False
def _lazy_load(self):
if not self._session_loaded():
self._cleanup()
self._load()
On Thu, Jan 20, 2011 at 6:36 AM, Franck <[email protected]> wrote:
> Dear all,
>
> When you enable sessions in web.py, the default behavior of the framework is
> to load/save the session into the store (e.g. a "sessions" table) at each
> request.
>
> Suppose you want a session management per request : i.e, some requests
> should be associated with the session, some other should not (for
> performance matters, for instance)
>
> Here's a sample recipe : let me know what you think !
> http://pastebin.com/Stn9uKvH
>
> Thanks !
> Franck
>
>
> """
> Per-request session management
> (web.py recipe)
> """
>
> import web
>
> def init_session():
> """ Initializes the session at a global level (web.config._session) """
>
> if web.config.get('_session') is None:
> db = web.database(dbn='sqlite', db='path_to_your_database')
> store = web.session.DBStore(db, 'sessions')
> # When building a Session, we must make sure that app is None,
> otherwise an application processor will be set up.
> # Such a global processor would load / save the session for every
> request, no matter what.
> web.config._session = web.session.Session(app = None, store = store,
> initializer = {'count': 0})
>
>
> def get_session():
> """ Returns the global session (web.config._session) """
> return web.config._session
>
>
> def _load_session():
> """ Cleans up the session and loads it from the store, by the id from
> cookie """
> session = get_session()
> session._cleanup()
> session._load()
>
> def _save_session():
> """ Saves the session into the store """
> get_session()._save()
>
> def enable_sessions(func):
> """ Wraps a controller method (GET/POST) in order to handle session
> management on a per-request basis """
>
> def wrapped_func(*args):
> _load_session()
> try :
> return func(*args)
> finally:
> _save_session()
>
> return wrapped_func
>
>
> class hello:
> """ Sample controller with a session-enabled GET method : see
> http://localhost:8080/ """
>
> @enable_sessions
> def GET(self):
>
> # get_session() may be freely called since GET is properly decorated
> session = get_session()
> print 'session', session
>
> # Also, the session may be freely modified
> session.count += 1
>
> return 'Hello ! Sessions are enabled : count = %d.' % session.count
>
> class anonymous:
> """ Sample controller with a session-disabled GET method : see
> http://localhost:8080/anonymous """
>
> def GET(self):
>
> # get_session() may not be called here ; actually, if called, the
> method will return an empty Storage
> return 'Hello ! Sessions are disabled.'
>
> # Defines the URLs
> urls = (
> '/', 'hello',
> '/anonymous', 'anonymous'
> )
>
> # Inits the web application
> app = web.application(urls, globals())
>
> # Initializes the session
> init_session()
>
> if __name__ == '__main__':
>
> # Runs the web application
> app.run()
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/webpy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.