[Twisted-web] Problem with twisted web and adbapi

Søren Bach Christiansen sbc at my-place.biz
Sat Jul 21 05:36:18 EDT 2007


Hi all

I am a new user to the Twisted framework so this might be a trivial  
question, but here goes..

Im trying to use the Web module and adbapi module to create a (rest)  
webservice but I am having problems
using the adbapi. As fare as I could figure out, the runInteraction  
call spawns a new thread and executes the sql in that, and the  
render_GET  handler is then returning right away -bummer. I need the  
adbapi for its db pool but dont need it to be "blocking" so that I  
can return the result of the db interaction as xml.

Hope you guys can help me out or give me some pointers if I am doing  
this the wrong way.


Regards
   Søren

----- Code ----
from twisted.web import server
from twisted.internet import reactor
from twisted.enterprise import adbapi
from xml.dom.minidom import Document

class Simple(Resource):
     isLeaf=True
     def __init__(self,opt):
         Resource.__init__(self)
         self.opt=opt

     def render_GET(self,request):
         doc=Document()
	self.opt.dbpool.runInteraction(self._getUserList,doc)
         print "returning result"
	return doc.toprettyxml(indent="  ")

     def _getUserList(self,txn,doc):
         txn.execute("SELECT UserName FROM User ")
         res = txn.fetchall()
         if (res):
             for row in res:
                 print row
         	usr=doc.createElement("user")
         	usr.setAttribute("name","%s" % (row[0] ))

class options:
     pass

if __name__ == '__main__':
     options.dbpool= adbapi.ConnectionPool("MySQLdb",  
host='localhost',db='TrackingPresentation',
                                    user='web', passwd='tingogsager')
     site = server.Site(Simple(options))
     reactor.listenTCP(8888,site)
     reactor.run()

------ Code ------





More information about the Twisted-web mailing list