【问题标题】:Twisted. Error callback on spawnProcess's processExited扭曲。 spawnProcess 的 processExited 上的错误回调
【发布时间】:2016-06-20 14:48:25
【问题描述】:

我正在使用 Twisted 的 ProcessProtocol 运行命令。

我正在使用文档脚手架,我有几个事件,例如 inConnectionLost、connectionMade 等

但是当进程完成时会触发这个事件:

def processExited(self, reason):
    print 'processExited, status %d' % (reason.value.exitCode,)
    if reason.value.exitCode == 0:
        print 'SUCCESS!!!'
    else:
        print 'ERROR!!!'
        raise RuntimeError, 'some custom error message'

如您所见,我试图引发错误以在更高级别捕获它。 我用这样的延迟来包装命令调用:

def success_pipeline(success):
    log.debug('The pipeline has finished correctly.')

def failure_pipeline(error, command):
    log.debug('The pipeline failed...')

command = CommandProtocol()
reactor.spawnProcess(command, application_bin, command_arguments, {})

d = Deferred()
d.addCallback(success_pipeline)
d.addErrback(failure_pipeline, command)
return d

但是,尽管命令失败(我手动输入错误的参数)它总是进入成功回调。 reason.value.exitCode 为 1,因此在 bash 中确认失败。

那么我怎样才能捕获一个错误返回来自定义处理失败呢?

【问题讨论】:

    标签: python twisted twisted.internet


    【解决方案1】:

    您可以将延迟对象交给CommandProtocol,让它从那里调用callbackerrback

    它记录在此页面的底部: http://twistedmatrix.com/documents/current/core/howto/process.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 2014-03-28
      • 2012-07-26
      相关资源
      最近更新 更多