Running out of memory w/ API requests
Posted: Tue Jan 10, 2017 7:09 pm
I'm using urequests.py to query an API endpoint every minute or so and retrieve JSON data. My script is usually successful on the first couple of tries, but eventually fails with a memory error. Is there something obvious I'm doing wrong? Is it possible the JSON data is being stored in memory somewhere and causing the script to eventually crash? Is there a command I can include at the end of my function to flush everything stored in memory?
Alternatively, is there a better way to retrieve JSON that uses less memory?
Here's a sample of the code (apologies in advance, I'm new to all of this):
import gc
import machine
import urequests
import json
import ht16k33_matrix
import bitmapfont
import time
gc.collect()
...
def myfunction():
array = []
api_data = urequests.get('http://apiwebsite.com/data&format=json').json()
for level2 in api_data["level2"]:
for level3 in level2["level3"]:
for level4 in level3["level4"]:
for level5 in level4["level5"]:
if (level2["Type"] == "Condition") and (level4["SecondType"] == "SecondCondition"):
array.append(int(level5["Characteristic"])/60)
api_data.close()
del api_data
gc.collect()
Alternatively, is there a better way to retrieve JSON that uses less memory?
Here's a sample of the code (apologies in advance, I'm new to all of this):
import gc
import machine
import urequests
import json
import ht16k33_matrix
import bitmapfont
import time
gc.collect()
...
def myfunction():
array = []
api_data = urequests.get('http://apiwebsite.com/data&format=json').json()
for level2 in api_data["level2"]:
for level3 in level2["level3"]:
for level4 in level3["level4"]:
for level5 in level4["level5"]:
if (level2["Type"] == "Condition") and (level4["SecondType"] == "SecondCondition"):
array.append(int(level5["Characteristic"])/60)
api_data.close()
del api_data
gc.collect()