Pystack Exhausted Help

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jasonpy
 
Posts: 2
Joined: Thu Jun 03, 2021 3:21 pm

Pystack Exhausted Help

Post by jasonpy »

Hello everyone! New here, but I'm running into an issue with a project and wondering if i could get some insights! I have a magtag that connects to a local network, requests Json data from a small server every 10 seconds, and then updates the display if that data has changed. The program runs great for about a minute and then I get a "RuntimeError: pystack exhausted" and the program stops. I'm wondering if there is anything I can do to avoid/solve this issue?

I found some resources online that suggest changing the CIRCUITPY_PYSTACK_SIZE in py/circuitpy_mpconfig.h to fix pystack exhausted errors. I was able to clone the adafruit/circuitpython repository and make this change to the file, however I'm not sure how to implement this updated circuitpython to my MagTag - 2.9" Grayscale E-Ink WiFi Display. I'm not sure if this is the best route to fix the issue but any resources or help in loading a custom version of circuitpython to my board would be appreciated!

I've attached my code below for reference (with some values missing). I have tried to make the logic as simple as I can to fix the pystack exhausted errors but nothing seems to be working.



Code: Select all

from adafruit_magtag.magtag import MagTag
import socketpool
import adafruit_requests
import ssl
import wifi
import gc


current = {'old_data': 'none', 'new_data': 'empty'}


wifi.radio.connect(<SSID>, <password>)

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

JSON_URL = <url>


magtag = MagTag()



def check_for_update():
    response = requests.get(JSON_URL)
    new_data = response.json()["tagdata1"]
    current.update({'new_data': new_data})

    

    if current['old_data'] == current['new_data']:
        gc.collect()
        magtag.enter_light_sleep(10)
        check_for_update()
    else:
        program()

def program():
    magtag.add_text(
        text_position=(
            (magtag.graphics.display.width // 2) -1,
            (magtag.graphics.display.height // 2) -1,
        ),
        text_scale=3,
        text_anchor_point=(0.5, 0.5),)

    magtag.set_text(current['new_data'])
    gc.collect()
    value = current['new_data']
    current.update({'old_data' : value})


    magtag.enter_light_sleep(10)
    check_for_update()


check_for_update()

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Pystack Exhausted Help

Post by dastels »

Yes. Don't use recursion. Python doesn't like it. Not even tail recursion.

Use a main while True loop instead of the recursive check_for_update() function. Also, note that check_for_update() and program(). are mutually recursive.

I'm surprised it lasts a whole minute.

Dave

User avatar
jasonpy
 
Posts: 2
Joined: Thu Jun 03, 2021 3:21 pm

Re: Pystack Exhausted Help

Post by jasonpy »

Thank you very much! Works great now! I didn't realize Python wasn't great with recursion.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Pystack Exhausted Help

Post by dastels »

Yes, that's a huge strike against it, IMO. But as long as you keep in mind that it's designed for iterative algorithms, it's all good.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”