【问题标题】:Cleanup of resources with the SMTP module of Twisted Python使用 Twisted Python 的 SMTP 模块清理资源
【发布时间】:2012-08-31 19:46:14
【问题描述】:

这与此处先前回答的问题有关:Logging SMTP connections with Twisted。我有一个在每个 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"

【问题讨论】:

  • 您要删除的资源是什么?是在 lambda 中创建的 ConsoleMessage() 吗?你怎么确定它没有被垃圾收集?如果没有调用 ConsoleMessageDelivery 上的 del 方法,这可能表明真正的问题是该对象永远不会被垃圾收集,因为有东西在持有该对象。
  • 我在 ConsoleMessageDelivery 的构造函数中实例化了一个对象,除此之外,它还打开了与数据库的连接。当 SMTP 连接关闭时,我需要确保数据库连接也关闭,以避免资源匮乏。有趣的是,如果我不使用任何包装工厂(如上面的 DenyFactory,也包括 TimeoutFactory),则 __del__() 方法会按预期调用。这可能是包装工厂的泄漏吗?

标签: python twisted smtpd


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回复。我现在理解并相应地重写了我的代码。我不知道__del__() 被认为是有害的。我有最后一个问题,link 然后我会尽量避开你的头发。 :)
  • 其实,已经回答了一个:) 虽然让-保罗可能会给你一个更好的答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
相关资源
最近更新 更多