我正在为 twisted 编写自定义 react 器,当它必须停止时需要做一些清理工作。 我尝试像这样重写停止方法:
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)
最佳答案
不要这样做。不要子类化 react 堆,不要重写它的方法。如果您想在 react 器停止时运行代码,请使用 reactor.addSystemEventTrigger("before", "shutdown", f)
(或 "during"
shutdown 或 “之后”
关机)。
或者使用更高级别的 API 并定义一个 Service
使用 stopService
方法并将您的服务连接到 application
.
关于python - 扭曲 react 器的拆卸方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090020/