[Twisted-Python] Twisted Enterprise (newbie question)
Steve Waterbury
steve.waterbury at gsfc.nasa.gov
Thu May 23 13:35:31 EDT 2002
Glyph Lefkowitz wrote:
>
> From: Steve Waterbury <steve.waterbury at gsfc.nasa.gov>
> Subject: Re: [Twisted-Python] Twisted Enterprise (newbie question)
> Date: Thu, 23 May 2002 11:36:48 -0400
>
> > So I created a "runQuery" method for adbapi.ConnectionPool:
> >
> > def runQuery(self, *args, **kw):
> > d = defer.Deferred()
> > apply(self.query, (d.callback,d.errback)+args, kw)
> > return d
> >
> > ... and now my script works! (Has adbapi.ConnectionPool been fixed yet
> > in the CVS version?)
>
> You probably want to be using adbapi.Augmentation; which, curiously enough, has
> a method "runQuery" ...
>
>
> def runQuery(self, *args, **kw):
> d = defer.Deferred()
> apply(self.dbpool.query, (d.callback, d.errback)+args, kw)
> return d
>
> ... that looks an awful lot like what you want :-) ...
Oops ... I *was* using Augmentation (as did the example):
Steve Waterbury wrote:
> Here's the new version of my script (assumes the process owner's
> login name is also a database user):
> --------------------------------------------------------------------------
> from twisted.internet import main
> from twisted.enterprise import adbapi
> import sys
> class TestDatabase(adbapi.Augmentation):
> ...
... but when I looked in adbapi, I forgot which one I was looking at!
My script works just fine with the original adbapi ... doh!
So adbapi is okay, but the example in "Introduction to Twisted Enterprise"
( http://twistedmatrix.com/documents/howto/enterprise ) should be
corrected, I think:
class AgeDatabase(adbapi.Augmentation):
"""A simple example that can retrieve an age from the database"""
def getAge(self, name):
# Define the query
sql = """SELECT Age FROM People WHERE name = ?"""
# Run the query, and return a Deferred to the caller to add
# callbacks to.
HERE >> return self.runOperation(sql, name)
^^^^^^^^^^^^
....................... should be "runQuery", in order for "resultlist" to work,
and ...
def gotAge(resultlist, name):
"""Callback for handling the result of the query"""
age = resultlist[0][0] # First field of first record
print "%s is %d years old" % (name, age)
HERE >>
db = MyDatabase(dbpool)
^^^^^^^^^^
........ should be "AgeDatabase"
# These will *not* block. Hooray!
db.getAge("Andrew").addCallbacks(gotAge, db.operationError,
callbackArgs=name).arm()
db.getAge("Glyph").addCallbacks(gotAge, db.operationError,
callbackArgs=name).arm()
-- Steve.
Stephen C. Waterbury http://misspiggy.gsfc.nasa.gov/people/waterbug.html
"An idiot with a computer is a faster, better idiot." - Rick Julius
More information about the Twisted-Python
mailing list