使用Twisted Python的SMTP模块清理资源



这与前面回答的问题有关:使用Twisted记录SMTP连接。我在ConsoleMessageDelivery的每个实例中都创建了一个数据库资源,我需要确保在套接字关闭时清理该资源。我有一个名为DenyFactory的WrappingFactory,当套接字关闭时会调用DenyFactory.unregisterProtocol方法,但我没有办法(我可以弄清楚)如何访问在正在销毁的ConsoleMessageDelivery实例中创建的资源。我在ConsoleMessageDelivery中尝试了del()方法,但从未调用过。在这种情况下,清理资源的最佳方法是什么?

class ConsoleMessageDelivery:
    implements(smtp.IMessageDelivery)
    def receivedHeader(self, helo, origin, recipients):
        myHostname, clientIP = helo
        headerValue = "by %s from %s with ESMTP ; %s" % (myHostname, clientIP, smtp.rfc822date())
        # email.Header.Header used for automatic wrapping of long lines
        return "Received: %s" % Header(headerValue)
    def validateFrom(self, helo, origin):
        # All addresses are accepted
        return origin
    def validateTo(self, user):
        if user.dest.local == "console":
            return lambda: ConsoleMessage()
        raise smtp.SMTPBadRcpt(user)
class ConsoleMessage:
    implements(smtp.IMessage)
    def __init__(self):
        self.lines = []
    def lineReceived(self, line):
        self.lines.append(line)
    def eomReceived(self):
        return defer.succeed(None)
    def connectionLost(self):
        # There was an error, throw away the stored lines
        self.lines = None
class ConsoleSMTPFactory(smtp.SMTPFactory):
    protocol = smtp.ESMTP
    def __init__(self, *a, **kw):
        smtp.SMTPFactory.__init__(self, *a, **kw)
        self.delivery = ConsoleMessageDelivery()
    def buildProtocol(self, addr):
        p = smtp.SMTPFactory.buildProtocol(self, addr)
        p.delivery = self.delivery
        return p
class DenyFactory(WrappingFactory):
    def buildProtocol(self, clientAddress):
        if clientAddress.host == '1.3.3.7':
            # Reject it
            return None
        # Accept everything else
        return WrappingFactory.buildProtocol(self, clientAddress)
    def unregisterProtocol(self, p):
        print "Unregister called"

首先,永远不要使用__del__,尤其是当您有一些资源需要清理时。__del__防止引用循环中对象的垃圾回收。(或者,切换到PyPy,它可以通过对循环中的对象集合强制执行任意排序来收集此类对象。)

接下来,考虑在消息传递工厂中打开数据库连接(或启动连接池),并在所有消息传递对象之间共享。这样,您就不需要清理的连接,因为您将在未来的消息中重复使用它们,而且您不会为每条消息分配一个新的连接,因此不会发生泄漏。

最后,如果您确实需要任何每事务对象,可以在IMessage对象上的eomReceivedconnectionLost实现中清理它们。一旦SMTP事务的DATA部分完成(因为接收到所有数据或因为连接丢失),就会调用其中一个方法。请注意,由于SMTP支持在单个事务中将邮件传递给多个收件人,因此可能有多个IMessage对象参与,即使只有一个IMessageDelivery对象。因此,您可能需要保留一个计数器,将消息传递对象上成功的validateTo调用的调用次数与消息对象上的eomReceived/connectionLost调用次数进行匹配。当每个调用的次数相同时,事务就完成了。

最新更新