Issue #510 resolved

@synchronize decorator useful (working diff included)

anonymous
created an issue

Here's a small diff (that I'm already using), providing session synchronization as a decorator. It's primarily useful for pages that need a consistent view of session data form beginning to end of page load.

{{{ Index: init.py =================================================================== RCS file: /cvs/third-party/CherryPy/cherrypy/init.py,v retrieving revision 1.1.1.3 diff -u -p -r1.1.1.3 init.py --- init.py 12 Apr 2006 13:30:47 -0000 1.1.1.3 +++ init.py 13 Apr 2006 21:05:23 -0000 @@ -90,6 +90,18 @@ def expose(func=None, alias=None): alias = func return expose_

+ +def synchronize(func): + '''A synchronize decorator, wrapping the function with session locks.''' + def _call_sync(*args, kwargs): + session.acquire_lock() + try: + return func(*args, kwargs) + finally: + session.release_lock() + return _call_sync + + def log(msg='', context='', severity=0, traceback=False): """Syntactic sugar for writing to the (error) log.""" # Load _cputil lazily to avoid circular references, and }}}

Comments (3)

  1. Robert Brewer

    This can be accomplished per-method in CherryPy 3 with the following:

    class Root:
        @tools.sessions(locking='implicit')
        def sessid(self):
            return cherrypy.session.id
        sessid.exposed = True
    

    CherryPy 2.x is in "bugfix only" mode.

  2. Log in to comment