Session Locking and Performance in CherryPy

  2007-09-20


While working on that firewall project I keep alluding to, I came across an interesting caveat when using sessions (tools.sessions) and static file directories (tools.staticdir):

Sessions are locked on a per-request basis meaning while a request is being processed, all other requests by that session will block until the lock is released (by default at the end of the response handler).

This is a Very Good Thing, at least as the default behavior. Its the only safe way to automatically handle sessions.

However, this can become a performance problem, especially for those of us addicted to tabbed browsing. Take this trivial app as an example:

#!/usr/bin/python
import cherrypy, time

class Test:
    @cherrypy.expose
    def page1(self):
        # Simulate long operation
        time.sleep(20)
        return "*Yawn*"

    @cherrypy.expose
    def page2(self):
        return "Hello World!"

cherrypy.config.update({
    'tools.sessions.on': True, 
    'tools.sessions.storage_type': 'ram'})
cherrypy.quickstart(Test(), '/')

Start it up, and then go to these pages in order in 2 different tabs:

  1. http://localhost:8080/page1
  2. http://localhost:8080/page2

You’ll notice that page2 doesn’t load until page1 is finished loading! This effectively limits sessions to 1 request at a time. For many web sites this might not be an issue. However, for complex web applications which might be doing time consuming operations on the server, this could give users the impression the server has crashed.

Fortunately this is a very easy issue to resolve. The best way is to do explicit session locking manually.

However, if implicit session locking works for you most of the time you can sneak in a cherrypy.session.release_lock() into your code Like this:

#...snip...
    @cherrypy.expose
    def page1(self):
        # Do any session manipulation here!
        cherrypy.session.release_lock()
        # Simulate long operation
        time.sleep(20)
        return "*Yawn*"
#...snip...

Now hit page1 and 2 again and notice that page2 loads immediately!

At Kevin’s suggestion I turned off the visual editor in WordPress. I had thought of this before, but couldn’t find where the option to disable it was! Evidently its a per-user setting which makes sense.

At any rate, WordPress seems to be leaving my whitespace alone now! Thanks Kevin!