[Twisted-Python] PBClientFactory and getRootObject

Fabrizio Mancini mr.file at gmail.com
Thu Apr 2 17:31:12 EDT 2009

Hi everyone,
I'm writing a server and a client using Perspective Broker with no Avatar
and no auth.
The server accumulates some data using a DeferredQueue.
The clients using a remote call retrieve one item per time from the queue
and elaborates it making some changes on a database using adbapi.
What i want to know is:
1) how can i recursively call the remote method to retrieve the items from
the server?
2) can i defer the database operations to a thread so every client can
elaborates multiple requests per time?

The code i'm using is this:

from twisted.internet import reactor, defer, pb
from twisted.enterprise import adbapi
from twisted.python import log
import os

globalConfigurationFile=os.path.abspath('conf' + os.sep +

class DataBasePreparerClient(object):

    def __init__(self, globalConfigurationFile):
        self._gcf = globalConfigurationFile
        self.data = {}
        self.d = defer.Deferred()
        self.clientfactory = pb.PBClientFactory()

    def _createDbPool(self):
        self.dbpool = adbapi.ConnectionPool('cx_Oracle',
self.data['db_username'], self.data['db_password'], self.data['db_tns'])

    def connect(self):
        reactor.connectTCP(self.data['server_ip'], self.data['server_port'],
        self.d = self.clientfactory.getRootObject()
        self.d.addCallbacks(self.get_item, self._eb)

    def _eb(self, reason):
        print "Failure: ", reason.getErrorMessage()

    def get_item(self, result):
        d = result.callRemote("get_item")

    def got_item(self, item):
        query = "update table where ..."
        res = self.dbpool.runOperation(query)
        self.d = self.clientfactory.getRootObject()
        self.d.addCallbacks(self.get_item, self._eb)

if __name__ == "__main__":

To call recursively the remote object in function connect i call
clientfactory.getRootObject() and addCallback() for the first time, and the
i recall always self.clientfactory.getRootObject() and self.d.addCallback
when the db query has completed.
Is this correct?
Do I have to always call self.clientfactory.getRootObject() every time i
have to call a remote method?
Can i deferToThread the function got_item or
Thanks in advance
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090402/871d5ba2/attachment.htm 

More information about the Twisted-Python mailing list