我有一个问题,正常尝试除了python中的块,如果有致命错误,你可以返回,ex ...
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
那么如何处理延迟,如果我只是返回它继续执行回调,认为错误已处理,但我如何告知用户出错的地方并破坏下游河链。
====更新===
mg谢谢你的帮助,但它没有用,即使有一个致命的错误,延迟仍然会返回到回调之后
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.")
答案 0 :(得分: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
。
我希望这个例子更具说明性。