“堆栈溢出错误”后如何自动重新运行python代码

问题描述

我已经编写了一段代码,它最终会得到一些想要的结果,但代码面临“堆栈溢出错误”的可能性非常高。

“堆栈溢出错误”后有没有办法自动重启代码

我尝试了“尝试”-和-“除外”,但没有奏效。

更新:

有人要了代码,我不能完全添加代码,但它是代码的最后一部分,它由一个函数组成,其中包含一系列函数函数总数需要等于团队数量[其中是一个列表] 减 1) 如果我用七个或八个函数运行代码,它会 100% 工作,但是当我运行具有系列中更多函数代码时,面临堆栈溢出错误的可能性将显着增加

会有一些可能的期望结果,但也有很多可能的方法来面对堆栈溢出错误

   def draw(destinationfile,teams):
       open(destinationfile,"w").close()
       all_matches(teams)
       draw_reg(1,destinationfile)
       draw_reg(2,destinationfile)
       draw_irreg(3,destinationfile)
       draw_reg(4,destinationfile)
       draw_irreg(5,destinationfile)
       draw_irreg(6,destinationfile)
       draw_reg(7,destinationfile)
       draw_irreg(8,destinationfile)
       draw_reg(9,destinationfile)
       draw_irreg(10,destinationfile)
       draw_irreg(11,destinationfile)
       draw_reg(12,destinationfile)
       draw_reg(13,destinationfile)
  draw("draw result.txt",leagueteams)

解决方法

我会这样做:

while True:
  try:
    foo()
  except:
    continue
  break;

如果失败(=在except子句中结束;抛出异常),它会转到循环的开头并再次尝试,否则,它只是从循环中中断。