python - 扭曲 react 器的拆卸方法?

标签 python twisted

我正在为 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/

相关文章:

pygtk - Twisted/PyGTK 的简单非网络示例

python - 使用扭曲和 (tx) 请求异步下载文件

python - 是否有任何理由使用 pass in有利而不是 None 作为空语句

python - 在 Python 中调用 Bing 或 IBM 文本转语音 API?

python - 有没有办法用预训练的 Inception-v3 网络对一批图像进行分类?

python - 将音频和视频 block 从 python 传输到 ffmpeg 的最佳方法

python - 我怎么知道 taskmgr 中运行的是哪个 python 脚本?

python - 我如何编写与扭曲服务器通信的东西?

python - 没有 errbacks 的 Twisted Deferred 错误如何通过试验进行测试?

python - 扭曲的 UDP 服务器 - 守护进程?