3

I'm writing a custom reactor for twisted and it needs some cleaning to do when it has to stop. I tried overriding the stop method like this:

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

It does, however, seem to not always be called. When I run trial like this python -m cProfile /usr/bin/trial -r custom tests/ | grep "stop" the only stop methods that are called are:

    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)
4

1 回答 1

3

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

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

于 2012-12-30T16:56:42.920 回答