如何从LoopingCall中停止TimerService

时间:2011-03-08 16:25:06

标签: python twisted

背景

我最近参与过使用过扭曲的项目。我们使用TimerService来守护进程。是的,我意识到这种方法可能过度,但我们正在努力保持一致并使用经过验证的框架。昨天,在LoopingCall中未处理异常导致TimerService失败,但是扭曲的应用程序仍在运行(参见twisted enhancement request)。为避免这种情况,我们希望在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.

注意:以下内容在callable中无效。


from twisted.internet import reactor
reactor.stop()

1 个答案:

答案 0 :(得分: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.
>>> 

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

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

TimerServiceLoopingCall的包装器。更具体地说,当它开始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)

产生这些结果:

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.

然而,这个工作正常:

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)

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

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)