On Wed, Aug 09, 2006 at 04:43:19PM +0800, wang wei wrote:
please have a look of fellow code.
from twisted.internet import reactor, protocol
class echo(protocol.Protocol): def __init__(self): from Queue import Queue self.q = Queue()
def lineReceived(self, line): self.q.put(line)
class echofactory(protocol.ServerFactory):
def buildProtocol(self, addr): p = echo() p.factory = self return p
class runEcho: def __init__(self): a = echofactory() reactor.listenTCP(1024, a) reactor.run()
My question is how can I get size of Queue q from another class when the program running.
That is a question I'm also interested in a good answer. My proposal would be to return the protocol object as a deferred. For me this works, but I'm not sure if this is a good approach. I extended wang wei's code so that the protocol data can be accessed by other classes. from twisted.internet import reactor, protocol, defer class echo(protocol.Protocol): def __init__(self): from Queue import Queue self.q = Queue() def connectionMade(self): self.factory.deferred.callback(self) def lineReceived(self, line): print line self.q.put(line) class echofactory(protocol.ServerFactory): def __init__(self): self.deferred = defer.Deferred() def buildProtocol(self, addr): p = echo() p.factory = self return p class runEcho: def connect(self): a = echofactory() a.deferred.addCallback(self.set_myprotocol) reactor.listenTCP(1024, a) reactor.run() def set_myprotocol(self,proto): print "protocol ready!" self.proto = proto print self.proto.q r = runEcho() r.connect() Can anyone give some comments on this? I am at the very beginning of twisted programming and I'm not sure if this is a good way to access the protocol class from the outside. best regards Torsten