[Twisted-Python] Design question: xmlrpc server - deferreds - adbapi

Remy C Cool remy.cool at smartology.nl
Sat Mar 12 10:49:12 EST 2005


Hello Twisters,

As a Twisted newbie, I'm not sure if, what i've coded is the Twisted way 
to go.

In this test, I've created a simple XMLRPC server

def main():
     # initialize factory
     factory = server.Site(XMLRPC())
     factory.resource.base = factory

     # initialize server
     reactor.listenTCP(8000, factory)

     try:
         try:
             reactor.run()
         except KeyboardInterrupt:
             print 'CTRL-C received, shutting down server.'
         except Exception, err:
             print 'Problem with running server: %s' % err
     finally:
         reactor.stop()


# The XMLRPC class:

class XMLRPC(xmlrpc.XMLRPC):

     def xmlrpc_test(self, text):
	d = defer.maybeDeferred(test, text)
         return d


# The test function

def test(text):
     # do something with the text
     return dbPool.pg.runInteraction(getTestData, text)	


# The getTestData function

def getTestData(cursor, text):
    # do queries using cursor
    return result

dbPool is a class which sets up dbpools for different databases 
according to a config file.

This setup seems to work, but I'm not sure about the way I'm using the 
deferreds.

Comments welcome :)

Grtz,
Remi Cool




More information about the Twisted-Python mailing list