[Twisted-web] Problem with twisted web and adbapi
Søren Bach Christiansen
sbc at my-place.biz
Sat Jul 21 10:22:00 EDT 2007
Thanks for the response Christian. Unfortunately im still not quite =
there yet. took you advice and change the code to read:
----- Snip ----
def render_GET(self,request):
def cb(text):
print text ### This text looks like it should
return text
return self.opt.dbpool.runInteraction(self._getUserList).addCallback =
(cb)
def _getUserList(self,txn):
doc=3DDocument()
camp=3D doc.createElement("userlist")
doc.appendChild(camp)
txn.execute("SELECT UserName FROM User ")
res =3D txn.fetchall()
if (res):
for row in res:
usr=3Ddoc.createElement("user")
camp.appendChild(usr)
usr.setAttribute("name","%s" % (row[0] ))
return doc.toprettyxml(indent=3D" ")
----- Snip ----
But still think the problem is basically the same as before. The =
runInteraction returns a Defered object that I
now add a callback function to. But the addCallback(cb) returns right =
away and the return value is still the Defered object. So the =
webserver gives an error saying something like:
Request did not return a string
Request:
<GET / HTTP/1.1>
Resource:
<__main__.Simple instance at 0x1208878>
Value:
<Deferred at 0x124cfa8>
Later when the _getUserList() returns the Defered object calles the cb =
() function and i get a printout with the wanted result.
So I think my problem is that I whant to *wait* for the result from =
the db query before the render_GET() returns.
/S=F8ren
On Jul 21, 2007, at 15:34 , Christian Simms wrote:
> 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):
> doc=3DDocument()
> def cb(PARAM_NOT_USED):
> print "returning result"
> return doc.toprettyxml(indent=3D" ")
> return self.opt.dbpool.runInteraction =
> (self._getUserList,doc).addCallback(cb)
>
> 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 =
> (self._getUserList).addCallback(cb)
>
> Cheers,
> Christian
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20070721/19=
eababc/attachment-0001.htm
More information about the Twisted-web
mailing list