Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 944

Show
Ignore:
Timestamp:
01/26/06 07:26:48
Author:
rdelon
Message:

Better document session.acquire_lock()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/filters/sessionfilter.py

    r942 r944  
    256256            f.close() 
    257257            return data 
    258         except IOError
     258        except IOError, EOFError
    259259            return None 
    260260     
  • trunk/docs/book/xml/sessions.xml

    r870 r944  
    173173    is serialized. This way, threads can't both modify the data at the same 
    174174    time and leave it in an inconsistent state.</para> 
    175     <para>You can easily make CherryPy serialize access to the session data by setting the <option>session_filter.locking</option> config option to <literal>implicit</literal> (the default is <literal>explicit</literal>, which means that CherryPy won't do any locking for you). In the <literal>implicit</literal> mode, 
    176     if a browser makes a second request while a first request is still being 
    177     handled by the server, the second request will block while the first 
    178     request is accessing the data. As soon as the first request is finished 
    179     then the second request will be able to access it.</para> 
    180     <para>This means that the second request will block until the first 
    181     request is finished.</para> 
     175    <para>What you need to do is call "cherrypy.session.acquire_lock()" in methods that update the session data. (Method that only read it don't need that call). The lock will be automatically released when the request is over. Here is a sample code that does it: 
     176    <screen> 
     177            class Root: 
     178                def increment_counter(self): 
     179                    # We call acquire_lock at the beginning 
     180                    #   of the method 
     181                    cherrypy.session.acquire_lock() 
     182                    c = cherrypy.session.get('counter', 0) + 1 
     183                    cherrypy.session['counter'] = c 
     184                    return str(c) 
     185                increment_counter.exposed = True 
     186 
     187                def read_counter(self): 
     188                    # No need to call acquire_lock 
     189                    #   because we're only reading 
     190                    #   the session data 
     191                    c = cherrypy.session.get('counter', 0) + 1 
     192                    return str(c) 
     193                read_counter.exposed = True 
     194            </screen></para> 
    182195  </section> 
    183196  <section id="callbacks"> 

Hosted by WebFaction

Log in as guest/cpguest to create tickets