[Twisted-Python] Closing connections properly

Christian Simms christian.simms at gmail.com
Sat Jan 19 09:41:58 EST 2008


On Jan 19, 2008 8:41 AM, Simon Pickles <sipickles at hotmail.com> wrote:
> Hi,
>
> Having a simple server based on a LineReceiver, what is the proper way
> to close connections and delete the class containing those connections
> (the class built by BuildProtocol()).
>
> Given the simple code below, Protocol.BuildProtocol is called when I
> client connections, and builds and instance of the class Connection.
> When the client disconnects, Connection.connectionLost is called, but
> the instance continues (__del__ is never called). I even naively tried
> added a 'del self' statement to the connectionLost method, but its not
> c++ 'delete this'!
>
> How should I delete the closed connection? Is there something in twisted
> I can overload, or should I use the server to track connections better
> and perform my deleting there?
>
> Thanks
>
> Simon
>
> Example
> --------------
>
> class Connection(basic.LineReceiver):
>     def __init__(self):
>         self.ipAddress = "000.000.000.000"
>         self.port = 0
>         logger.CON("Opening new connection")
>
>     def __del__(self): # never called
>         logger.CON("Closed connection to %s:%d" % (self.ipAddress,
> self.port))
>
>
>     def connectionMade(self):
>         """ Overrides method in basic.LineReceiver """
>         self.ipAddress = self.transport.getPeer().host
>         self.port = self.transport.getPeer().port
>         logger.CON("Log in attempt from %s:%d" % (self.ipAddress,
> self.port))
>
>     def connectionLost(self, reason):
>         """ Overrides method in basic.LineReceiver """
>         logger.CON("%s:%d has closed the connection, %s" %
> (self.ipAddress, self.port, reason))
>         self.transport.loseConnection()
>
>     def dataReceived(self, data):
>         """ Overrides method in basic.LineReceiver """
>         logger.CON("%s:%d received %s" % (self.ipAddress, self.port, data))
>
>
> class Server(Protocol):
>     factory = Factory()
>     factory.protocol = Connection
>     def __init__(self, maxClients):
>         self.max_clients = maxClients
>         logger.NET("Server started")
>
>     def doStart(self):
>         logger.NET("Port: %d" % globalVars.zoneAddress[1])
>         logger.NET("...... waiting for a connection ......")
>     def doStop(self):
>         logger.NET("Server is now closed")
>
>     def buildProtocol(self, addr):
>         logger.NET("Connection attempt....")
>         return Connection()
>
>
>
> --
> Linux user #458601 - http://counter.li.org.
>
>
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>

You should not rely on Python's garbage collector to track your open
connections, since you don't how long the garbage collector will wait
until it deletes your object. Instead, you should just track your own
list of connections in the Factory object.

Cheers,
Christian




More information about the Twisted-Python mailing list