【发布时间】:2017-03-18 10:44:36
【问题描述】:
当我尝试运行具有 600 次测试的智能卡测试工具的主机程序并且在第 300 次测试后出现此错误时,我不断收到此错误“RuntimeError:调用 Python 对象时超出最大递归深度”,我尝试了“sys .setrecursionlimit(10000)" 并解决了问题,但我知道这不是解决此错误的最佳方法,如何更改我的代码以免遇到此错误:
def SndRcv(self,request):
print ">> ", request
device_api.send(request)
resp = device_api.receive()
print "<< ", resp
self.processResponse(resp)
def processResponse(self, K400Message):
global mWaitingCardRemoval
ciMsg = card_interface_response
ciMsgType = card_interface_response.ci_msg
if ciMsgType is None:
print 'weird, malformed protobuf response'
return
whichMsg = ciMsgType.WhichOneof('msg')
print 'msg = ' + str(whichMsg)
if whichMsg is 'collision':
self.StartSession()
elif whichMsg is 'card_removed':
if ciMsgType.issuer== ci.CARD_INTERFACE_MASK_CxLESS:
mWaitingCardRemoval &= ~(ciMsgType.issuer)
if EndofSession is False:
self.parseMessage()
if mWaitingCardRemoval !=0:
self.parseMessage()
self.StartSession()
elif whichMsg is 'waiting_removal':
if EndofSession is False:
self.parseMessage()
else:
mWaitingCardRemoval |= ciMsgType.issuer
elif whichMsg is 'card_detected':
mode = ciMsgType.issuer
reqMsg = pm.get_Deactivate((ci.CARD_INTERFACE_MASK_ANY)& ~(ciMsgType.issuer))
self.SendOnly(reqMsg)
acceptMsg = pm.get_Activate(mode)
self.SndRcv(acceptMsg)
elif whichMsg is 'card_ready':
self.StartLoop(ciMsgType.issuer)
elif whichMsg is 'rapdu':
self.processCardAPDUResponse(ciMsgType.issuer, ciMsg.data.encode('hex'))
elif whichMsg is 'card_not_responding':
if ciMsgType.issuer == ci.CARD_INTERFACE_MASK_CONTACT:
self.EndCardSession(ciMsgType.issuer,True)
else:
self.EndCardSession(ciMsgType.issuer, False)
elif whichMsg is 'resp_special':
if ciMsg.data.encode('hex') > 0:
logging.info(ciMsg.data.encode('hex'))
else:
logging.info("")
【问题讨论】:
-
self.SndRcv调用self.processResponse,self.processResponse调用self.SndRcv。你能明白为什么这会导致任意深度递归吗? -
要了解递归,您首先需要了解递归...
-
扩展一点:
SndRcv永远不会返回,processResponse只会返回if ciMsgType is None。 -
您也许可以将它们实现为生成器/协程 - you'll have to learn a bit - 这让我想起了 a trampoline。
-
@PM2Ring 是的,我可以看到,我是 python 编程的新手,不知道如何重构它以使其像现在一样工作(除了错误),