[Twisted-Python] Adding persistent db connections to twisted web server?
Lewin, Karl E
Karl.Lewin at BNSF.com
Thu Jun 12 07:12:29 MDT 2003
What is the best/accepted/preferred approach to adding a pool of db
connections that can be re-used in *.rpy's?
My first attempt is to add have a class like this:
# DBPool.py
from twisted.enterprise import adbapi
from MySQLdb import cursors
class genericQuery(adbapi.Augmentation):
def __init__(self):
adbapi.Augmentation.__init__(self,
adbapi.ConnectionPool"MySQLdb",
host='localhost',db='xxxxx',
cursorclass=cursors.DictCursor))
def getData(self,sql,parms=None):
if parms==None:
return self.runQuery(sql)
else:
return self.runQuery(sql,parms)
And make a copy of web.py with the following added to the updateApplication
function:
root.registry.setComponent(genericQuery,genericQuery())
(with the proper imports added as well) and then test it with something
like:
#test.rpy
from twisted.enterprise import adbapi
from twisted.web import resource,server
from DBPool import genericQuery
class MyGreatResource(resource.Resource):
def render(self, request):
q = registry.getComponent(genericQuery)
d = q.getData("select version()")
d.addCallback(self.returnResult,request)
return server.NOT_DONE_YET
def returnResult(self, resultlist, request):
request.write(`resultlist`)
request.finish()
resource = MyGreatResource()
I realize that there can be some more paramaterization of the
dbmodule/database/etc parameters but I haven't don't that yet.
Is that a decent way of doing this(or rephrased, how stupid is this way of
doing this)? How an *.rpy that needed to perform multiple queries work? Is
there an example of using twisted.web and twisted.enterprise together
anywhere?
Thanks-
Karl
More information about the Twisted-Python
mailing list