[Twisted-Python] Multiple client connections
Stan Benrsteen
sbernste at mitre.org
Wed Sep 6 17:12:29 EDT 2006
All,
I want to write a client that connects to a Twisted Server using pb.
I want my client to make several connections. For example:
from twisted.spread import pb
from twisted.internet import defer
from twisted.internet.tcp import Connector
from twisted.internet import reactor
PORT = 8992
class ModelCalculator:
def __init__(self, host, port):
self.host = host
self.port = port
def add(self, a, b):
self.factory = pb.PBClientFactory()
deferred = self.factory.getRootObject()
deferred.addCallback(self.connected, a, b)
deferred.addErrback(self.failure)
reactor.run()
return self.result
def connected(self, perspective, a, b):
deferred = perspective.callRemote('calculate', a, b)
deferred.addCallback(self.success, perspective)
deferred.addErrback(self.failure)
def success(self, result, perspective):
self.result = result
self.stopReactor()
def failure(self, reason):
self.result = None
self.stopReactor()
def stopReactor(self):
reactor.stop()
if __name__ == '__main__':
calculator = ModelCalculator("127.0.0.1", PORT)
print calculator.add(4,5)
print calculator.add(14,5)
print calculator.add(24,5)
print calculator.add(34,5)
As you see, I want to add many numbers and print out the result.
This doesn't work because the reactor is been stopped in the
stopReactor() method that is invoked in either success() or failure
(). How can I change it so I can call my remote add() method many
times without waiting for the reactor to be stopped? Is there a way
that I can use the PB stuff without the reactor?
thanks,
/amn
More information about the Twisted-Python
mailing list