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

James R. Saker Jr. jsaker at americanrelay.com
Mon May 10 09:13:02 MDT 2004


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)








More information about the Twisted-Python mailing list