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

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 def printData(d): "Got to printData" print d def printError(failure): import sys sys.stderr.write(str(failure)) if __name__ == '__main__': g = Getter() d = g.getData('Joe') d.addCallback(printData) d.addErrback(printError) reactor.callLater(4, reactor.stop); reactor.run() ##------------postgresql database table info---------------------------- mydb=# select * from myuser; pkid | name | age ------+--------+----- 1 | Joe | 36 2 | Sally | 15 3 | Peanut | 5 (3 rows) mydb=# SELECT age FROM myuser WHERE name = 'Joe'; age ----- 36 (1 row)

On Mon, 2004-05-10 at 11:13, James R. Saker Jr. wrote:
I think I'm missing something rather basic and would greatly appreciate any suggestions.
You're never calling self.d.callback() with a value, and thus self.d never fires. Perhaps you are interested in the Deferred returned from runQuery, not creating your own?

James R. Saker Jr. wrote:
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/

On Mon, 2004-05-10 at 11:13, James R. Saker Jr. wrote:
I think I'm missing something rather basic and would greatly appreciate any suggestions.
You're never calling self.d.callback() with a value, and thus self.d never fires. Perhaps you are interested in the Deferred returned from runQuery, not creating your own?

James R. Saker Jr. wrote:
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/
participants (3)
-
Christopher Armstrong
-
Glyph Lefkowitz
-
James R. Saker Jr.