我正在使用 Plex 并尝试使用 Plex api 来获取并持续更新我的 Plex 服务器的状态。如果状态发生变化,我会做一些事情。这是我的代码(我已经编辑了不相关的部分):
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 this part. Redacted code
if '<MediaContainer size="0">' in data.text: # If nothing is playing
media_type = 'None'
state = 'STOPPED'
else: # Get what's playing and its 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: # THIS IS IMPORTANT. I don't want to execute the next part if nothing has changed.
# DO STUFF, redacted
if state == 'PLAYING':
interval = 1
elif state == 'PAUSED':
interval = 10
elif state == 'STOPPED':
interval = 15
else:
interval = 60 * 3 # if nobody home, only check once every 3 minutes
time.sleep(interval)
get_status(media_type, state)
get_status('a', 'b') # filler arguments used to initiate script
问题
不幸的是,半小时左右后,我得到:
RecursionError:比较中超出了最大递归深度
这是回溯(尽管行号由于密文而不会匹配):
Traceback (most recent call last):
File "H:/Users/micha/Documents/Scripts/Python/plexStatus.pyw", line 63, in <module>
p = psutil.Process(os.getpid()) # Get PID for this 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
问题
如何让它在没有递归错误的情况下工作,同时在执行之间仅等待 1 秒?
我的猜测是,有一种更好的方法来调用此脚本,同时仍然能够使用先前调用中的变量。我就是不知道怎么办。我想我可以使用我已经在另一个编辑部分中使用的pickle,但如果可能的话,我想避免大量的读/写。或者实际上,在保留功能的同时,尽可能减少对内存的占用。
注释: 我确信有更好的解析方法。我是编程新手。在我修复这个问题的同时进行研究。
在尝试修复此问题时,我已多次编辑此代码,因此我不确定这是否会引发其他错误。这是我试图修复的递归。
我知道有像 PlexPy 这样的工具可以做到这一点。只是想自己写一个更小的,我可以连续运行而不是一个主要的资源消耗者。
最佳答案
递归意味着每个函数调用都保留在堆栈上,因此您最终将耗尽内存,从而导致RecursionError
。在其他方面,有一些方法可以使用“尾部调用递归”来解决这个问题,几乎就像你所做的那样 - except Python will never support this .
运行此程序的最佳方法是修改代码以具有无限循环:
def get_status(last_media_type, last_state):
pass # your method here
# except for the 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 # do what you need to compare them here
if last_state != state and last_media_type != media_type:
print "they aren't equal!"
last_state, last_media_type = state, media_type
循环本身 (while True:
) 几乎不会消耗任何内存,现在您只需保存最后一个状态,而不是存储每个过去的状态。
此外,方法调用中的任何信息都会被垃圾收集,因为当方法返回时它会失去作用域 - 这是对代码的直接改进,因为在递归调用中没有任何东西永远失去作用域,所以什么也没有cal 已被收集。
关于每秒无限运行的 Python HTTP 请求,并与之前的迭代进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049624/