Hi everyone,<br>I'm writing a server and a client using Perspective Broker with no Avatar and no auth.<br>The server accumulates some data using a DeferredQueue.<br>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.<br>
What i want to know is: <br>1) how can i recursively call the remote method to retrieve the items from the server?<br>2) can i defer the database operations to a thread so every client can elaborates multiple requests per time?<br>
<br>The code i'm using is this:<br><br>from twisted.internet import reactor, defer, pb<br>from twisted.enterprise import adbapi<br>from twisted.python import log<br>import os<br><br>globalConfigurationFile=os.path.abspath('conf' + os.sep + 'configuration.ini')<br>
<br>class DataBasePreparerClient(object):<br> '''<br> DataBasePreparerClient()<br> '''<br> <br> def __init__(self, globalConfigurationFile):<br> self._gcf = globalConfigurationFile<br>
self.data = {}<br> self._parseConfig()<br>
self._createDbPool()<br> self.d = defer.Deferred()<br> self.clientfactory = pb.PBClientFactory()<br> <br> def _createDbPool(self):<br> self.dbpool = adbapi.ConnectionPool('cx_Oracle', self.data['db_username'], self.data['db_password'], self.data['db_tns'])<br>
<br> def connect(self):<br> reactor.connectTCP(self.data['server_ip'], self.data['server_port'], self.clientfactory)<br> self.d = self.clientfactory.getRootObject()<br> self.dbpool.connect()<br>
self.d.addCallbacks(self.get_item, self._eb)<br><br> def _eb(self, reason):<br>
print "Failure: ", reason.getErrorMessage()<br>
<br>
def get_item(self, result):<br>
d = result.callRemote("get_item")<br>
d.addCallback(self.got_item)<br>
<br> def got_item(self, item):<br> query = "update table where ..."<br> res = self.dbpool.runOperation(query)<br> res.addErrback(self._eb)<br> self.d = self.clientfactory.getRootObject()<br>
self.d.addCallbacks(self.get_item, self._eb)<br> <br>if __name__ == "__main__":<br> DataBasePreparerClient(globalConfigurationFile).connect()<br> reactor.run()<br><br>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.<br>
Is this correct?<br>Do I have to always call self.clientfactory.getRootObject() every time i have to call a remote method?<br>Can i deferToThread the function got_item or self.dbpool.runOperation(query)?<br>Thanks in advance<br>
Fabrizio<br>