python线程中的错误 [英] Bug in python thread

查看:190
本文介绍了python线程中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些raspberry pi运行一些python代码.有时我的设备将无法检入.其余的python代码继续正常运行,但此处的代码退出了.我不确定为什么吗?如果设备无法签入,则应重新启动,但不能重启. python文件中的其他线程继续正常运行.

I have some raspberry pi running some python code. Once and a while my devices will fail to check in. The rest of the python code continues to run perfectly but the code here quits. I am not sure why? If the devices can't check in they should reboot but they don't. Other threads in the python file continue to run correctly.

class reportStatus(Thread):
    def run(self):
        checkInCount = 0
        while 1:
            try:
                if checkInCount < 50:
                    payload = {'d':device,'k':cKey}
                    resp = requests.post(url+'c', json=payload)
                    if resp.status_code == 200:
                        checkInCount = 0
                        time.sleep(1800) #1800
                    else:
                        checkInCount += 1
                        time.sleep(300) # 2.5 min
                else:
                    os.system("sudo reboot")
            except:
                try:
                    checkInCount += 1
                    time.sleep(300)
                except:
                    pass

这些设备可以运行数天和数周,并且每30分钟就会进行一次完美的检查,然后突然停止运行.我的linux计算机处于只读状态,并且计算机可以继续工作并正常运行.我的问题在这个线程中.我认为他们可能没有得到回应,这行可能是问题

The devices can run for days and weeks and will check in perfectly every 30 minutes, then out of the blue they will stop. My linux computers are in read-only and the computer continue to work and run correctly. My issue is in this thread. I think they might fail to get a response and this line could be the issue

resp = requests.post(url+'c', json=payload)

我不确定如何解决此问题,我们将不胜感激任何帮助或建议.

I am not sure how to solve this, any help or suggestions would be greatly appreciated.

谢谢

推荐答案

光秃秃的 except:pass是一个非常糟糕的主意.

A bare except:pass is a very bad idea.

一种更好的方法是至少记录所有异常:

A much better approach would be to, at the very minimum, log any exceptions:

import traceback

while True:
  try:
    time.sleep(60)
  except:
    with open("exceptions.log", "a") as log:
      log.write("%s: Exception occurred:\n" % datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
      traceback.print_exc(file=log)

然后,当您遇到异常时,您将获得一条日志:

Then, when you get an exception, you get a log:

2016-12-20 13:28:55: Exception occurred:
Traceback (most recent call last):
  File "./sleepy.py", line 8, in <module>
    time.sleep(60)
KeyboardInterrupt

您的代码也有可能挂在 sudo reboot 超时(来自链接的答案):

It is also possible that your code is hanging on sudo reboot or requests.post. You could add additional logging to troubleshoot which issue you have, although given you've seen it do reboots, I suspect it's requests.post, in which case you need to add a timeout (from the linked answer):

import requests
import eventlet
eventlet.monkey_patch()


#...
resp = None
with eventlet.Timeout(10):
    resp = requests.post(url+'c', json=payload)
if resp:
    # your code

这篇关于python线程中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆