Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Pystack Exhausted Help
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pystack Exhausted Help

by jasonpy on Thu Jun 03, 2021 3:34 pm

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 | TOGGLE FULL SIZE
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()

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

Re: Pystack Exhausted Help

by dastels on Thu Jun 03, 2021 4:19 pm

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

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

Re: Pystack Exhausted Help

by jasonpy on Thu Jun 10, 2021 4:54 am

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

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

Re: Pystack Exhausted Help

by dastels on Thu Jun 10, 2021 8:07 am

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

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

Please be positive and constructive with your questions and comments.