[Twisted-Python] Missing something basic on defer and postgresql

Christopher Armstrong radix at twistedmatrix.com
Mon May 10 09:41:28 MDT 2004


James R. Saker Jr. wrote:
> I think I'm missing something rather basic and would greatly appreciate
> any suggestions.
> 
> After working through the Twisted RDBMS example (with no luck - the
> second RDBMS example gets a deferred but fails to run
> .addCallback(printResult), I thought I'd read more about deferreds in
> its own section and made a simple postgresql example using the third
> example in the Deferreds section. 
> 
> When I run this (with database = postgresql, dbname = mydb, table =
> myuser), it yields the following:
> 
> Got to getData
> got to gotResults with name = Joe
> 
> ...but never runs addCallback(self, _toHTML). Apparently I'm missing
> something pretty obvious about callbacks - and may be the same issue I
> dealt with in the RDBMS example. Any suggestions? 
> 
> jamie


> ##~ begin dbserver.py
> from twisted.enterprise import adbapi
> from twisted.internet import reactor, defer
>                                                                                                                     
> dbpool = adbapi.ConnectionPool("psycopg", 'dbname=mydb user=postgres')
>                                                                                                                     
> class Getter:
>    def gotResults(self, name):
>       print "got to gotResults with name = %s" % name
>       return dbpool.runQuery("SELECT age FROM myuser WHERE name = '%s'"
> % name)
>                                                                                                                     
>    def _toHTML(self, r):
>       print "Got to _toHTML"
>       print "Result is: %s" % r
>                                                                                                                     
>    def getData(self, x):
>       self.d = defer.Deferred()
>       print "Got to getData"
>       reactor.callLater(2, self.gotResults, x)
>       self.d.addCallback(self._toHTML)
>       return self.d

Yeah, this is wrong. You've got two Deferreds here: the one you cerated 
manually in getData, and the one that runQuery returns. You're *throwing 
away* the one that runQuery returns, and you're never triggering a 
callback on the one that you're creating manually. It doesn't seem that 
there's a reason to create your own Deferred, anyway, so replace getData 
with this:

def getData(self, x):
     d = dbpool.runQuery("...") # no need for self.d, afaics
     d.addCallback(self._toHTML)
     return d

And get rid of gotResults.

-- 
  Twisted | Christopher Armstrong: International Man of Twistery
   Radix  |          Release Manager,  Twisted Project
---------+           http://radix.twistedmatrix.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 256 bytes
Desc: OpenPGP digital signature
URL: </pipermail/twisted-python/attachments/20040510/ae6a100c/attachment.sig>


More information about the Twisted-Python mailing list