| 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> |
|---|