[Twisted-Python] Twisted Enterprise (newbie question)

Steve Waterbury steve.waterbury at gsfc.nasa.gov
Thu May 23 11:36:48 EDT 2002


Andrew Bennetts wrote:
> 
> On Wed, May 22, 2002 at 01:38:31PM -0400, Steve Waterbury wrote:
> > Twisted Gurus,
> >
> > I'm just starting to experiment with Twisted, and am trying out the
> > example in the "Introduction to Twisted Enterprise", slightly modified:
> 
> <snip>
> 
> Was that your complete script?

It was.  

> If so, it also needs to start the Twisted main-loop:
> ---
> from twisted.internet import main
> main.run()
> ---

Thanks -- that helped a lot!  :^)

> [Or for the version in CVS:
> from twisted.internet import reactor
> reactor.run()
> ]

Good to hear that "main" got the name-change!

> The Twisted Enterprise docs should probably be updated to make this
> clearer, rather than assuming that people are already familiar with the
> Twisted main-loop...

I might be able to help some with the Twisted Enterprise docs, since 
I hope to use it pretty intensively in our app ... 

> I'd suggest also reading "Writing Twisted Servers".  It's by far the
> best general overview of Twisted we have at the moment, even if you
> don't intend to write servers with it.

I'd read that at least twice, but I guess I hadn't grokked it.  
But now that I know the main loop needs to be run explicitly, 
it makes perfect sense ... how else would I get the result of 
callback?  I'm learning ...

> Let us know if you any other problems.

Right:  I got a traceback that said my "result" was not subscriptable.  
Looking at the code for adbapi.ConnectionPool in Twisted 0.17.4, 
I noticed that the "runOperation" method used in the example ultimately
calls _runOperation, which says it "is used for non-query operations 
that don't want "fetch*" to be called" ... doh!  No wonder my "result" 
was not subscriptable!  ;^)  

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?)   

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):
    def getSchemaByName(self,schemaname):
        sql = """SELECT identification from schema WHERE schema_name = %s"""
        return self.runQuery(sql,schemaname)

def gotSchemaByName(resultlist):
    ident = resultlist[0][0]
    print resultlist
    print "Schema has identifier %s." % ident

dbpool = adbapi.ConnectionPool("pyPgSQL.PgSQL",database="test")
db = TestDatabase(dbpool)

db.getSchemaByName("pgpdm_schema").addCallbacks(gotSchemaByName, db.operationError).arm()
main.run()
--------------------------------------------------------------------------

... with the output:
--------------------------------------------------------------------------
[['PGPDM-1.0']]
Schema has identifier PGPDM-1.0.
--------------------------------------------------------------------------

Thanks for the help!
-- 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