Bug in python thread

2024/10/5 16:32:56

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 = 0while 1:try:if checkInCount < 50:payload = {'d':device,'k':cKey}resp = requests.post(url+'c', json=payload)if resp.status_code == 200:checkInCount = 0time.sleep(1800) #1800else:checkInCount += 1time.sleep(300) # 2.5 minelse:os.system("sudo reboot")except:try:checkInCount += 1time.sleep(300)except:pass

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.

Thank you

Answer

A bare except:pass is a very bad idea.

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

import tracebackwhile 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

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
https://en.xdnf.cn/q/119674.html

Related Q&A

how does a function changes the value of a variable outside its scope? Python

i was coding this code and noticed something weird, after my function has been called on the variable, the value of the variable gets changed although its outside of the functions scope, how exactly is…

Python extracting element using bs4, very basic thing I think I dont understand

So Im using Beautiful Soup to try to get an element off of a page using the tag and class. Here is my code: import requests from bs4 import BeautifulSoup# Send a GET request to the webpage url = "…

Why Isnt my Gmail Account Bruteforcer Working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.Improve…

Python: Split Start and End Date into All Days Between Start and End Date

Ive got data called Planned Leave which includes Start Date, End Date, User ID and Leave Type.I want to be able to create a new data-frame which shows all days between Start and End Date, per User ID.S…

Python and java AES/ECB/PKCS5 encryption

JAVA VERSION:public class EncryptUtil {public static String AESEncode(String encodeRules, String content) {try {KeyGenerator keygen = KeyGenerator.getInstance("AES");keygen.init(128, new Secu…

How to find the center point of this rectangle

I am trying to find the center point of the green rectangle which is behind the fish, but my approach is not working. Here is my code:#Finding contours (almost always finds those 2 retangles + some noi…

Simple Battleships game implementation in Python

Okay Im not sure how to develop another board with hidden spaces for the computers ships per-se, and have it test for hits. Again Im not even sure how Im going to test for hits on the board I have now.…

How to remove WindowsPath and parantheses from a string [duplicate]

This question already has an answer here:Reference - What does this regex mean?(1 answer)Closed 4 years ago.I need to remove WindowsPath( and some of the closing parentheses ) from a directory string.…

How to escape escape-characters

I have a string variable which is not printing properly, I guess because it contains escape characters. How to escape these escape-characters?>>> print "[p{Aa}\\P{InBasic_Latin}\r\t\n]&q…

Python Multiprocessing a large dataframe on Linux

As shown in the title, I have a big data frame (df) that needs to be processed row-wise, as df is big (6 GB), I want to utilize the multiprocessing package of python to speed it up, below is a toy exam…