[Twisted-Python] protocol-factory question

Thomas Hervé therve at free.fr
Fri Jun 22 05:44:22 EDT 2007

> So I wrote my Protocol class, and my Factory class. If I understood the
> behavior of
> this system, every time a client connects to my server, factory build a
> protocol instance and
> "attach" it to the client. So the client comunicates with that protocol
> instance, right?
> Now, when from protocol I call self.transport.loseConnection(in way to
> disconnnect itself),
> the client will be disconnected,but its protocol instance still lives
> on. What can I do
> to destroy protocol instance after its client disconnection? Because if
> I have a thousand of
> connection attempts, I'll have a thousand of protocol instances, with a
> lot of allocated resources

Protocol instance will remain alive in your program if you keep a  
reference to it. By default it's not done within Twisted, so when the  
connection is closed, the protocol instance must be destroyed at the  
next garbage collect turn, without doing anything in your application.

If you keep a reference to it (to have a list of connected clients for  
example), just remove your instance from this list in connectionLost.


More information about the Twisted-Python mailing list