1
我有一個Twisted客戶端應用程序,每分鐘可創建數百個連接。我發現我的應用程序有一個內存泄漏,我幾乎可以肯定它與從未刪除的ClientFactory()派生類有關。扭曲的客戶端內存泄漏
我重現該問題與回聲客戶端的例子的從扭曲文檔的修改:
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()
有了這個代碼我發現()EchoClientFactory的實例僅刪除程序停機時。連接完成後,它們不會被刪除。如果我需要做一些工廠實例刪除,我還沒有在文檔中找到。
你原來的實現是否覆蓋__del __()方法?如果是這樣,你應該問自己爲什麼你需要這樣做。 99%的案例是一個壞主意。 我在我的扭曲應用程序memleaks我有很多問題,我發現objgraph庫非常有用的跟蹤他們。 –