如何从LoopingCall中停止TimerService

bit*_*cle 0 python twisted

背景

我最近参与了一个使用扭曲的项目.我们使用TimerService来守护进程.是的,我意识到这种方法可能过度,但我们试图保持一致并使用经过验证的框架.昨天,在LoopingCall中未处理异常导致TimerService失败,但是扭曲的应用程序仍在运行(参见扭曲增强请求).为避免这种情况,我们希望在catch-all异常处理程序结束时停止服务.

题

如何从LoopingCall可调用方法中停止TimerService和Twistd应用程序?我担心的是,当TimerService无法处理异常时,linux进程会继续运行,即使TimerService不再循环也是如此.

例如:


def some_callable():
  try:
    # do stuff
  except SomeSpecificError ex:
    # handle & log error
  except SomeOtherSpecificError ex:
    # handle & log error
  except:
    # log sys.exc_info() details
    # stop service.
Run Code Online (Sandbox Code Playgroud)

注意:以下内容在可调用内不起作用.


from twisted.internet import reactor
reactor.stop()
Run Code Online (Sandbox Code Playgroud)

Jea*_*one 5

你无法在反应堆启动前停止它:

>>> from twisted.internet import reactor
>>> reactor.stop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/base.py", line 570, in stop
    "Can't stop reactor that isn't running.")
twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
>>> 
Run Code Online (Sandbox Code Playgroud)

但是,只要反应堆已经运行,reactor.stop工作正常:

>>> from twisted.internet import reactor
>>> reactor.callLater(3, reactor.stop)
<twisted.internet.base.DelayedCall instance at 0xb762d2ec>
>>> reactor.run()
[... pause ...]
>>> 
Run Code Online (Sandbox Code Playgroud)

TimerService是一个包装LoopingCall.更具体地说,当它开始时LoopingCall,它会传递now=True给它run.这会导致函数立即第一次被调用,而不是在指定的间隔过去一次之后.

所以当TimerService.startService调用时,调用你的函数.反应堆还没有运行.在第一次调用您的函数时,您无法停止反应堆,因为它尚未启动.

这个程序:

from twisted.application.internet import TimerService

def foo():
    from twisted.internet import reactor
    reactor.stop()

from twisted.application.service import Application

application = Application("timer stop")
TimerService(3, foo).setServiceParent(application)
Run Code Online (Sandbox Code Playgroud)

产生这些结果:

exarkun@boson:/tmp$ twistd -ny timerstop.tac
2011-03-08 11:46:19-0500 [-] Log opened.
2011-03-08 11:46:19-0500 [-] using set_wakeup_fd
2011-03-08 11:46:19-0500 [-] twistd 10.2.0+r30835 (/usr/bin/python 2.6.4) starting up.
2011-03-08 11:46:19-0500 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
2011-03-08 11:46:19-0500 [-] Unhandled Error
        Traceback (most recent call last):
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/application/service.py", line 277, in startService
            service.startService()
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/application/internet.py", line 284, in startService
            self._loop.start(self.step, now=True).addErrback(self._failed)
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/task.py", line 163, in start
            self()
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/task.py", line 194, in __call__
            d = defer.maybeDeferred(self.f, *self.a, **self.kw)
        --- <exception caught here> ---
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/defer.py", line 133, in maybeDeferred
            result = f(*args, **kw)
          File "timerstop.py", line 5, in foo
            reactor.stop()
          File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/base.py", line 570, in stop
            "Can't stop reactor that isn't running.")
        twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
Run Code Online (Sandbox Code Playgroud)

但是,这个工作正常:

from twisted.application.internet import TimerService

counter = 0

def foo():
    global counter
    if counter == 1:
        from twisted.internet import reactor
        reactor.stop()
    else:
        counter += 1

from twisted.application.service import Application

application = Application("timer stop")
TimerService(3, foo).setServiceParent(application)
Run Code Online (Sandbox Code Playgroud)

稍微不那么严重,这个也是如此:

from twisted.application.internet import TimerService

def foo():
    from twisted.internet import reactor
    reactor.callWhenRunning(reactor.stop)

from twisted.application.service import Application

application = Application("timer stop")
TimerService(3, foo).setServiceParent(application)
Run Code Online (Sandbox Code Playgroud)