2

我正在使用 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: maximum recursion depth exceeded in comparison

这是回溯(尽管由于修订,行号不匹配):

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 这样的工具。只是想自己写一个更小的,我可以连续运行而不会成为主要的资源消耗者。

4

2 回答 2

2

递归意味着每个函数调用都保留在堆栈上,因此您最终将耗尽内存导致RecursionError. 在其他有使用“尾调用递归”的方法,几乎​​就像你所做的一样 -除了 Python 永远不会支持 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。

于 2016-01-27T22:56:46.547 回答
1

您可以使用 while 循环来进行无限循环,而不是使用递归。递归不是无限的,因为正如您所看到的 - 递归增加了堆栈,因此是有限的。

于 2016-01-27T22:54:04.343 回答