[Twisted-web] Problem with twisted web and adbapi

Christian Simms christian.simms at gmail.com
Sat Jul 21 09:34:40 EDT 2007

On 7/21/07, S=F8ren Bach Christiansen <sbc at my-place.biz> wrote:
> 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=F8ren
> ----- 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=3DTrue
>      def __init__(self,opt):
>          Resource.__init__(self)
>          self.opt=3Dopt
>      def render_GET(self,request):
>          doc=3DDocument()
>         self.opt.dbpool.runInteraction(self._getUserList,doc)
>          print "returning result"
>         return doc.toprettyxml(indent=3D"  ")
>      def _getUserList(self,txn,doc):
>          txn.execute("SELECT UserName FROM User ")
>          res =3D txn.fetchall()
>          if (res):
>              for row in res:
>                  print row
>                 usr=3Ddoc.createElement("user")
>                 usr.setAttribute("name","%s" % (row[0] ))
> class options:
>      pass
> if __name__ =3D=3D '__main__':
>      options.dbpool=3D adbapi.ConnectionPool("MySQLdb",
> host=3D'localhost',db=3D'TrackingPresentation',
>                                     user=3D'web', passwd=3D'tingogsager')
>      site =3D server.Site(Simple(options))
>      reactor.listenTCP(8888,site)
>      reactor.run()
> ------ Code ------
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

You did all the hard work already.  The only thing you need to do is connect
the database query to the render_GET rendering method's output. You want to
take the Deferred that ruInteraction returns (yes it runs in a separate
thread async), add a callback to it that converts the xml to a string
(called cb below), then return that Deferrred in render_GET.  Here's
untested code:

     def render_GET(self,request):
         def cb(PARAM_NOT_USED):
             print "returning result"
             return doc.toprettyxml(indent=3D"  ")
         return self.opt.dbpool.runInteraction

If I was writing this code, I would probably have _getUserList create and
return the doc, in which case the code would look like this:

     def render_GET(self,request):
         def cb(doc):  # now doc is passed to the callback from _getUserList
             print "returning result"
             return doc.toprettyxml(indent=3D"  ")
         return self.opt.dbpool.runInteraction

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20070721/dc=

More information about the Twisted-web mailing list