【问题标题】:python 3.x - pyserial - stack overflow fatal errorpython 3.x - pyserial - 堆栈溢出致命错误
【发布时间】:2018-05-14 07:27:02
【问题描述】:

我写了一个继承自 serial.Serial 对象的对象并添加了一些方法。在无限循环中,程序从串行连接中获取信息。当有人关闭另一侧的 Divice 时,SerialException 会通知用户出现问题并再次调用该函数。在大约 300 个错误之后,我得到一个堆栈溢出致命错误。

def endless():
 try:
  with serialObjectDerivedFromserial.Serial("/dev/tty/bsp") as bsp:
  #doing somthing
 except serial.SerialException:
  #notify not connected
  time.sleep(10)
  endless()
 except:
  #notify error

【问题讨论】:

    标签: python fatal-error pyserial


    【解决方案1】:

    您从自身调用endless。虽然它不是被禁止的(它是递归的基础),但每次调用都会消耗一点堆栈。因此,在多次调用后出现堆栈溢出也就不足为奇了。正确的做法是将递归改为迭代处理:

    def endless():
      while True:
         try:
          with serialObjectDerivedFromserial.Serial("/dev/tty/bsp") as bsp:
          #doing somthing
         except serial.SerialException:
          #notify not connected
          time.sleep(10)
          continue               # keep on looping on SerialException
         except:
          #notify error
         break                   # exit loop on any other case
    

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2011-02-08
      • 2012-09-18
      • 2014-01-26
      • 2019-02-16
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多