尽管我正在检查反应堆是否已在运行,但出现ReactorNotRestartable错误

时间:2019-04-10 16:17:20

标签: twisted twisted.internet

代码:

def hook(ui, repo, hooktype, node=None, source=None, **kwargs):

    from buildbot.clients import sendchange
    from twisted.internet import defer, reactor

    for master in masters:
        s = sendchange.Sender(master, auth=(username, password))
        d = defer.Deferred()
        reactor.callLater(0, d.callback, None)

        for rev in range(start, end):
            # some code here
            d.addCallback(dummy, "calling dummy")

    def _printSuccess(res):
        pass

    def _printFailure(why):
        pass

    d.addCallbacks(_printSuccess, _printFailure)
    d.addBoth(lambda _: reactor.stop())
    try:
        logger.info("before starting reactor")
        if not reactor.running:
            reactor.run()
            logger.info("after reactor.run")
        else:
            logger.info("reactor is already running")

    except Exception as e:
        logger.info("in exception")
        logger.exception("exception occurred")
        if reactor.running:
            reactor.stop()


def dummy(content):
    with open("/home/rhodecode/hg_buildbot_hooks/dummy.txt", "w") as f:
        f.write("dummy" + "\n")
        f.write("{}".format(content))

错误:

File "/home/rhodecode/hg_buildbot_hooks/hooks.py", line 291, in hook
    reactor.run()
File "/home/rhodecode/buildbot_venv/lib/python2.7/site-packages/twisted/internet/base.py", line 1266, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
File "/home/rhodecode/buildbot_venv/lib/python2.7/site-packages/twisted/internet/base.py", line 1246, in startRunning
    ReactorBase.startRunning(self)
File "/home/rhodecode/buildbot_venv/lib/python2.7/site-packages/twisted/internet/base.py", line 754, in startRunning
    raise error.ReactorNotRestartable()
ReactorNotRestartable

2 个答案:

答案 0 :(得分:1)

出于两个不同的原因,我在两个不同的地方面对这个问题

  1. 反应堆由python主线程启动,但一个非主线程正在调用reactor.callFromThread(reactor.stop),并且没有反应堆正在停止。 refreactor.run()

  2. 来自非主线程的
  3. Reactor as reactor.run(installSignalHandlers=False)被调用,并且不是事件开始。 ref<template name="new_footer_default" inherit_id="website.footer_default"> <xpath expr="//div[@id='footer']" position="replace"> <div id="footer"> <div class="mynewdiv">$0</div> </div> </xpath> </template>

答案 1 :(得分:0)

它是ReactorNotRestartable。关键字可能是可重新启动。您不能多次启动扭曲反应堆-即使在两次尝试之间将其停止也是如此。