recursion - Python HTTP Request that runs every second, infinitely, and compares to previous iteration -
i'm using plex , trying use plex api , continually update status of plex server. if status changes, have couple of things does. here's code (i've redacted irrelevant portions):
import requests import time ip = '127.0.0.1' port = '####' def get_status(last_media_type, last_state): data = requests.get('http://' + ip + ':' + port + '/status/sessions', verify=false) if home: # ignore part. redacted code if '<mediacontainer size="0">' in data.text: # if nothing playing media_type = 'none' state = 'stopped' else: # what's playing , status media_type_start = data.text.find('type=\"') media_type_end = data.text.find('updatedat=') media_type = data.text[media_type_start + 6:media_type_end - 2].capitalize() print('type: ' + str(media_type)) state_prefix_start = data.text.find('<player address=') state_prefix_end = data.text.find('<transcodesession key=') state_prefix = data.text[state_prefix_start:state_prefix_end] state_start = state_prefix.find('state=\"') state_end = state_prefix.find('title=') state = state_prefix[state_start + 7:state_end - 2].upper() if last_media_type != media_type or last_state != state: # important. don't want execute next part if nothing has changed. # stuff, redacted if state == 'playing': interval = 1 elif state == 'paused': interval = 10 elif state == 'stopped': interval = 15 else: interval = 60 * 3 # if nobody home, check once every 3 minutes time.sleep(interval) get_status(media_type, state) get_status('a', 'b') # filler arguments used initiate script the problem
unfortunately, after half hour or so, get:
recursionerror: maximum recursion depth exceeded in comparison
this traceback (although line numbers won't match due redaction):
traceback (most recent call last): file "h:/users/micha/documents/scripts/python/plexstatus.pyw", line 63, in <module> p = psutil.process(os.getpid()) # pid script file "c:\python351\lib\site-packages\psutil\__init__.py", line 349, in __init__ self._init(pid) file "c:\python351\lib\site-packages\psutil\__init__.py", line 375, in _init self.create_time() file "c:\python351\lib\site-packages\psutil\__init__.py", line 636, in create_time self._create_time = self._proc.create_time() file "c:\python351\lib\site-packages\psutil\_pswindows.py", line 282, in wrapper return fun(self, *args, **kwargs) file "c:\python351\lib\site-packages\psutil\_pswindows.py", line 422, in create_time if self.pid in (0, 4): recursionerror: maximum recursion depth exceeded in comparison the questions
how can work without recursion error while still waiting 1 second between executions?
my guess there's better way call script, while still being able use variables previous call. can't figure out how. think can use pickle i'm using in redacted part, i'd avoid ton of read/writes if possible. or really, make little of memory hog possible while still retaining functionality.
notes: i'm sure there's better way parse. i'm new programming. researching concurrently fix this.
i've edited code few times while trying fix this, i'm not sure won't throw other error. it's recursion i'm trying fix.
i know there tools plexpy this. trying write 1 myself smaller , can run continuously without being major resource hog.
recursion means each function call stays on stack, hence run out of memory leading recursionerror. in other there ways around using "tail-call recursion", you've done - except python never support this.
the best way run modify code have infinite loop:
def get_status(last_media_type, last_state): pass # method here # except recursive call return state, media_type last_state = none last_media_type = 'sensible_default' while true: state, media_type = get_status(last_media_type, last_state) pass # need compare them here if last_state != state , last_media_type != media_type: print "they aren't equal!" last_state, last_media_type = state, media_type the loop (while true:) consume practically no memory, , instead of storing every single past state, have last one.
additionally, information in method call garbage collected loses scope when method returns - direct improvement on code, in recursive call nothing ever loses scope nothing cal ever collected.
Comments
Post a Comment