【问题标题】:Tear down method for twisted reactor?扭曲电抗器的拆卸方法?
【发布时间】:2012-12-14 22:18:30
【问题描述】:

我正在为 twisted 编写一个自定义反应器,当它必须停止时需要进行一些清洁。 我尝试像这样覆盖停止方法:

def stop(self):
    posixbase.PosixReactorBase.stop(self)
    #cleanup code here

然而,它似乎总是被调用。当我像这样python -m cProfile /usr/bin/trial -r custom tests/ | grep "stop" 运行试验时,唯一调用的 stop 方法是:

    2    0.000    0.000    0.000    0.000 abstract.py:397(stopReading)
    1    0.000    0.000    0.000    0.000 abstract.py:405(stopWriting)
    1    0.000    0.000    0.000    0.000 log.py:691(stop)
    1    0.000    0.000    0.000    0.000 protocol.py:678(stopProtocol)
    3    0.000    0.000    0.000    0.000 reporter.py:97(stopTest)
    3    0.000    0.000    0.000    0.000 result.py:79(stopTest)
    1    0.000    0.000    0.000    0.000 udp.py:218(stopListening)

【问题讨论】:

    标签: python twisted


    【解决方案1】:

    不要这样做。不要子类化反应器,不要覆盖它的方法。如果你想在反应器停止时运行代码,请使用reactor.addSystemEventTrigger("before", "shutdown", f)(或"during"shutdown 或"after"shutdown)。

    或者使用更高级别的 API 并使用 stopService 方法定义 Service 并将您的服务连接到 application

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2018-03-25
      • 2011-08-10
      相关资源
      最近更新 更多