Twisted中的致命错误与Deferred,如何停止Deferred
我遇到了一个问题。在Python中,正常的try except块可以在出现严重错误时直接返回,比如...
try:
logon()
except 404_Error:
retry_logon(try = 2)
except AuthenticationProblem:
error_label.SetText( "You password or username was wrong. Make sure that CAPS LOCK key is not on." )
return#nothing more we can do here
else:
#display user information or whatever
那么在使用deferreds的时候该怎么做呢?如果我直接返回,程序会继续执行回调,认为错误已经处理了。但我该如何通知用户出了问题,并且终止后续的操作呢?
==== 更新 ===
谢谢你的帮助,但这方法不管用,即使出现了严重错误,defer仍然会在之后返回到回调。
from twisted.internet import reactor
from twisted.internet.defer import Deferred as D
class NonFatalError(Exception):
'non fatal error'
class FatalError(Exception):
'fatal error'
def c(s):
print "Callback called"
print "Data Received: %s" % s
def e(f):
print "Errorback Called"
print "Error Type: %s" % type(f)
print "Traceback"
f.printTraceback()
print "======================================="
f.trap(NonFatalError)
return "Error Handled"
def e_fatal(f, d):
print "Errorback Called"
print "Error Type: %s" % type(f)
print "Traceback"
f.printTraceback()
print "======================================="
print "Fatal Error"
f.trap(FatalError)
return "Fatal Error... Crash and die. No more callbacks should be called."
def trigger():
d.errback(FatalError("This error is fatal to the defer"))
if __name__ == "__main__":
d = D()
d.addErrback(e)
d.addErrback(e_fatal, d)
d.addCallback(c)
d.addCallback(c)
d.addCallback(c)
d.addCallback(c)
reactor.callLater(3, trigger)
reactor.callLater(10, reactor.stop)
reactor.run()
raw_input("Done.")
1 个回答
2
好的,我来给你解释一下 deferreds 是怎么工作的。你可以把程序的流程想象成一个状态机。成功或者失败就像是这个机器的输入,可能会改变它的状态。在你的例子中,有两个状态:已登录和未登录,还有三个输入:成功登录、认证错误和因为服务器问题无法登录。只有一个输入是可以恢复的,那就是服务器因为某些奇怪的问题无法登录用户,在这种情况下,你可以通过重试登录来解决问题。下面是新的代码:
import sys
from twisted.internet import reactor, defer
class FourOhFourError(Exception):
pass
class AuthenticationError(Exception):
pass
def logon(retry=3, success=2, wrong_auth=0):
# do stuff
d = defer.Deferred()
# not_found is the only error recoverable
d.addErrback(not_found, retry, success)
if wrong_auth:
reactor.callLater(0, d.errback, AuthenticationError("wrong auth"))
else:
if success == 0:
reactor.callLater(0, d.callback, "Mario")
else:
reactor.callLater(0, d.errback, FourOhFourError("Not found"))
return d
def not_found(failure, retry, success):
failure.trap(FourOhFourError) # this is superfluous here
print failure.getErrorMessage()
if retry == 0:
raise AuthenticationError("Max retries")
# do stuff
print "retring..."
d = defer.Deferred()
d.addCallback(logon, success-1)
reactor.callLater(1, d.callback, retry-1) # not really clean here
return d
def wrong_auth(failure):
failure.trap(AuthenticationError) # this is superfluous here
# do stuff
print "something goes wrong"
print failure.getErrorMessage()
def loggedIn(user):
print "hello %s" % user
def stop(_):
reactor.stop()
d = logon(*map(int, sys.argv[1:]))
d.addCallbacks(loggedIn, wrong_auth)
d.addBoth(stop)
reactor.run()
调用这个代码时需要三个参数:最大重试次数、在第几次重试时系统应该登录用户,第三个是一个布尔值,表示用户凭证是否正确。你可以尝试以下调用: 0 0 1
、3 2 0
、3 4 0
。
希望这个例子能更清楚地说明问题。