🎁📬❄️📦 Holiday Shipping Deadlines are approaching! International customers and domestic ground - get your orders in 12/9/2020 📦❄️📬🎁
0

How to write a robust main loop
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to write a robust main loop

by plinth on Fri Oct 30, 2020 5:25 pm

I'm writing code for pyportal to ping a server every 20 minutes and pull data down.
Every day or so, the code crashes with a RuntimeException. The problem is that I'm doing the check inside a try/except that explicitly catches RuntimeException, but it never gets hit.

Here's my main loop:
Code: Select all | TOGGLE FULL SIZE
while True:
    try: # this is line 90
        values = pyportal.fetch()
        label_deaths.text = "Deaths: {}".format(values[0])
        label_newdeaths.text = "Deaths Today: {}".format(values[1])
    except RuntimeError as e:
        print ("failure getting data. Waiting a bit and trying again: ", e)
        sleep (10000)
        continue
    except:
        print ("unknown error getting data. Waiting a bit and trying again.")
        continue

    # update every 20 minutes
    time.sleep(60 * 20)


And here is the stack trace after a failure:
Code: Select all | TOGGLE FULL SIZE
Retrieving data...Traceback (most recent call last):
  File "code.py", line 90, in <module>
  File "adafruit_pyportal.py", line 874, in fetch
  File "adafruit_requests.py", line 291, in get
  File "adafruit_requests.py", line 251, in request
  File "adafruit_requests.py", line 235, in request
  File "adafruit_esp32spi/adafruit_esp32spi_socket.py", line 102, in readline
RuntimeError: Didn't receive full response, failing out


Clearly my handler isn't being called - how do I make this rock solid?

plinth
 
Posts: 3
Joined: Fri Oct 30, 2020 5:20 pm

Re: How to write a robust main loop

by dastels on Fri Oct 30, 2020 6:06 pm

First, are you using the latest version of CircuitPython (6.0.0-rc.0) and the latest version modules from the latest 6.0 bundle?

Dave

dastels
 
Posts: 4251
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to write a robust main loop

by plinth on Sun Nov 01, 2020 3:25 pm

5.x, which was the latest I could find when I started the project a week ago and is dated 10/17 (adafruit-circuitpython-bundle-5.x-mpy-20201017).
I note that 6.0 is a release candidate, not a release.

plinth
 
Posts: 3
Joined: Fri Oct 30, 2020 5:20 pm

Re: How to write a robust main loop

by plinth on Fri Nov 20, 2020 11:06 am

Upgrading to 6rc2 seems to have fixed the issue.

plinth
 
Posts: 3
Joined: Fri Oct 30, 2020 5:20 pm

Please be positive and constructive with your questions and comments.