【问题标题】:python script crashes after long time runningpython脚本长时间运行后崩溃
【发布时间】:2026-01-26 02:10:01
【问题描述】:

我有一个在 Raspberry Pi 3 上运行的 python 2.7 脚本。

class UIThread(threading.Thread):

   def __init__(self, threadID, name, counter, U):

    threading.Thread.__init__(self)

    self.threadID = threadID

    self.name = name

    self.counter = counter

    self.U = U

  def run(self):

    self.U.run()

def main():

  time.sleep(3)

  try:
     try:
         ###launch a UI running as background thread#####
         U = UIlib.UI()
         thread1 = UIThread(1, "UI", 1, U)
         thread1.daemon = True
         thread1.start()

     except:
         ###if there is no monitor, lanch a fake UI class#######
         U = UIlib.nomonitorUI()
         thread1 = UIThread(1, "NMUI", 1, U)
         thread1.daemon = True
         thread1.start()

         print "No Monitor detected"
         pass

    ####perform interaction with the BQ chip, contain a while true loop######
     char_balan(U)

  except:
    e = sys.exc_info()
    print e
    print "UI exit"

它的基本功能是通过 UART 向芯片发送消息,获取响应消息,更新日志文件并将其打印到 UI(由 python curses 创建的监视器上显示的 UI)。它每 1 秒执行一次。

脚本运行 32 小时没有错误,然后崩溃。 UI 崩溃并被错误消息覆盖:“无法打开 shsh:加载共享库时出错:libc.so.6:无法打开共享对象文件...” python脚本

我检查了树莓派的内存状态。 python 进程在第 32 小时使用了大约 1/4 的总内存。所以不是内存导致崩溃。另外,我尝试在没有监视器的情况下运行它,这将启动一个没有 python.curses 的假 UI 类。同样的崩溃发生在第 32 小时。

现在,我不知道脚本为什么会崩溃。

【问题讨论】:

  • @StevenRumbalski 是的,这是一个错字。
  • 您使用的是哪个版本的libc,还是glibc
  • @jmunsch 我对此不太了解。我所做的只是将一个 python 脚本加载到一个带有 Ras* 的树莓派 3 上。而且我对linux系统没有做任何事情。
  • 每隔几个小时检查一次lsof -i $PID的输出,是不是打开了太多的文件描述符?错误退出代码是24吗?
  • 您还有这个问题吗?你对jmunsch的问题有答案吗?我可以在您的问题中添加赏金以使其引起注意,但我想确保您随时可以回答问题。

标签: python python-2.7 uart raspberry-pi3 python-curses


【解决方案1】:

我有一堆 8 个树莓派用作种子箱。我遇到了同样的错误,我从我的一位 raspi 开发人员朋友那里得到的最接近的官方回答是,一些较旧的内核与硬件存在一些不兼容的错误。更新到最新的 Pixel 版本将解决您的问题。

【讨论】:

  • 感谢您的建议。我通过改变程序结构解决了这个问题。即使程序现在没有崩溃,但现在运行速度要慢得多。