gpt4 book ai didi

python - Twisted 中的 fatal error 和延迟,停止延迟

转载 作者:行者123 更新时间:2023-12-01 06:11:13 25 4
gpt4 key购买 nike

我有一个问题,使用 python 中的正常 try except block ,如果出现 fatal error ,则可以返回,例如...

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 来做到这一点,如果我只是返回它就会执行回调,认为错误已得到处理,但我如何通知用户出了问题并破坏了下游链。

====更新===

mg 感谢您的帮助,但它不起作用,即使出现 fatal error ,延迟仍然返回到回调后的内容

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.")

最佳答案

好的,一个全新的答案可以更好地解释如何 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()

使用三个参数调用代码:最大重试次数,系统应重试用户登录的次数,第三个参数是一个 bool 值,指示用户凭据的正确性。尝试以下调用:0 0 13 2 03 4 0

我希望这个例子更具说明性。

关于python - Twisted 中的 fatal error 和延迟,停止延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5809900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com