Ken Kinder's personal website

MySQL (MySQLdb) sessions on CherryPy

No such module existed, so I wrote one. Using it is pretty simple:

"""
Example code using mysqlsession.py
"""
from mysqlsession import MySQLSession
import cherrypy
import logging
 
logging.basicConfig(level=logging.DEBUG)
 
sessionInfo = {
    'tools.sessions.on': True,
    'tools.sessions.storage_type': "Mysql",
    'tools.sessions.connect_arguments': {'db': 'sessions'},
    'tools.sessions.table_name': 'session'
}
 
cherrypy.config.update(sessionInfo)
 
class HelloWorld:
    def index(self):
        v = cherrypy.session.get('v', 1)
        cherrypy.session['v'] = v+1
        return "Hello world! %s" % v
 
    index.exposed = True
 
cherrypy.quickstart(HelloWorld())

Download the module: mysqlsession.py

I would publish it formally, but I’m going to see if I can just have it added to the CherryPy project.

UPDATE: I took out the table locking and used InnoDB select-for-update locking.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">