撕裂反应器的拆解方法?

时间:2012-12-30 10:40:51

标签: python twisted

我正在编写一个用于扭曲的定制反应器,当它必须停止时需要进行一些清洁。 我尝试重写这样的stop方法:

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)

1 个答案:

答案 0 :(得分:3)

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

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

相关问题