Saturday, February 17, 2007

threadsafe CheryPy

Using CherryPy is so easy, like everything in Python. But when running some pages with heavy SQL processing time, there are a lot of funny errors (malloc.. double free). Ooops, looks as something is not theadsafe. mySQLdb is not necessarily threadsafe, but cherrypy.engine.on_start_thread_list should handle this as suggested . Instead of digging deep into debugging, here is an alternative fix, using DBUtils.

Yipee, my code got posted into CherryPy:

import cherrypy
import MySQLdb

from DBUtils.PersistentDB import PersistentDB

class HomePage:
@cherrypy.expose

def index(self):
sql = "select max(whatever) from bigtable;" #long running query

c = persDb.connection().cursor()
c.execute(sql)

res = c.fetchone()
c.close()
return ' '.join( ['<html>', repr(res), '</html>'] )


cherrypy.tree.mount(HomePage())

if __name__ == '__main__':
persDb=PersistentDB(MySQLdb, 10000, host='localhost', db='db', passwd='****')

cherrypy.config.update({'server.thread_pool': 10})
cherrypy.server.quickstart()

cherrypy.engine.start()





No comments: