【发布时间】: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