[Twisted-Python] Twisted client memory leak

Diego Woitasen diego at woitasen.com.ar
Tue Jan 22 06:18:13 MST 2013


Here is the server code if you want to have a test:

from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet import reactor


class Echo(Protocol):

    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):
        print 'MADE'

    def connectionLost(self, reason):
        print 'LOST'

    def dataReceived(self, data):
        self.transport.write(data)

class EchoFactory(Factory):
    def buildProtocol(self, addr):
        return Echo(self)

reactor.listenTCP(8007, EchoFactory())

reactor.run()

Thanks!

On Tue, Jan 22, 2013 at 10:06 AM, Diego Woitasen <diego at woitasen.com.ar> wrote:
> I have an Twisted client app that makes hundreds of connections per
> minute. I discover that I have a memory leak un my app and I'm almost
> sure that is related to the ClientFactory() derived class that is
> never deleted.
>
> I reproduce the problem with a modification of Echo client example
> from Twisted documentation:
>
>     from twisted.internet.protocol import Protocol, ClientFactory
>     from twisted.internet import reactor
>     from twisted.internet.task import LoopingCall
>
>     from sys import stdout
>
>     class Echo(Protocol):
>         def connectionMade(self):
>             print 'MADE'
>             self.transport.write('XXXX')
>
>         def dataReceived(self, data):
>             print 'RECV', data
>             self.transport.loseConnection()
>
>         def __del__(self):
>             print 'DEL PROTOCOL'
>
>     class EchoClientFactory(ClientFactory):
>         def startedConnecting(self, connector):
>             print 'Started to connect.'
>
>         def buildProtocol(self, addr):
>             print 'Connected.'
>             return Echo()
>
>         def clientConnectionLost(self, connector, reason):
>             print 'Lost connection.  Reason:', reason
>
>         def clientConnectionFailed(self, connector, reason):
>             print 'Connection failed. Reason:', reason
>
>         def __del__(self):
>             print 'DEL FACTORY'
>
>     def connector():
>         print 'CONNECTOR'
>         factory = EchoClientFactory()
>         reactor.connectTCP('localhost', 7, factory)
>
>     #reactor.callLater(2, connector)
>     register_loop = LoopingCall(connector)
>     register_loop.start(1)
>
>     reactor.run()
>
> With this code I discover that the instances of EchoClientFactory()
> are only deleted when the program shutdowns. They are not deleted when
> the connections finish. I haven't found in the documentation if I need
> to do some to get factory instances deleted.
>
> --
> Diego Woitasen



-- 
Diego Woitasen




More information about the Twisted-Python mailing list