How To Kill Twisted Protocol Instances Python
I have a server application written in python using twisted and I'd like to know how to kill instances of my protocol (bottalk). Everytime I get a new client connection, I see the
Solution 1:
You closed the TCP connection by calling loseConnection
. But there's no code anywhere in your application that removes items from the clients
list on the factory.
Try adding this to your protocol:
defconnectionLost(self, reason):
Factory.clients.remove(self)
This will remove the protocol instance from the clients
list when the protocol's connection is lost.
Also, you should consider not using the global Factory.clients
to implement this functionality. It's bad for all the usual reasons globals are bad. Instead, give each protocol instance a reference to its factory and use that:
class botfactory(Factory):
def buildProtocol(self, addr):
protocol = bottalk()
protocol.factory = self
return protocol
factory = botfactory()
factory.clients = []
StandardIO(factory.buildProtocol(None))
reactor.listenTCP(8123, factory)
Now each bottalk
instance can use self.factory.clients
instead of Factory.clients
.
Post a Comment for "How To Kill Twisted Protocol Instances Python"