You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plex服务器监控Python脚本检测到更新时卡顿故障求助

Plex服务器监控脚本卡顿问题修复

问题描述

在树莓派上运行Python脚本,通过外接显示器监控Plex服务器,显示当前流、转码数量及更新状态。但检测到更新可用时,脚本卡顿,无法实时更新信息。尝试将root.after替换为末尾的while循环定时调用update(),问题依旧。

问题分析

原代码存在几个关键问题:

  • Text组件操作错误:使用textv['text']=textv.delete("1.0", "end")这种错误写法,delete方法不需要赋值,直接调用即可。
  • 内容插入目标错误:更新状态的文本被错误插入到trans_stream组件,而非update_avail,导致UI混乱。
  • 定时器逻辑冗余:在if/else分支中重复调用root.after,且while True + time.sleep会阻塞tkinter的主事件循环,导致UI冻结。
  • API调用阻塞UI:plex.isLatest()需要发起网络请求,耗时较长,直接在UI线程调用会导致卡顿。

修复后的完整代码

# Import the required library
from tkinter import *
from plexapi.server import PlexServer
import threading
import time

baseurl = 'SERVERIP'
token = 'PLEXTOKEN'
plex = PlexServer(baseurl, token)

root = Tk()
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
root.geometry("1920x200")

# 创建UI组件
textv = Text(root, width=200, height=15, fg='red', bg='black', 
             font=('helvetica',30), highlightthickness=0, borderwidth=0)
textv.place(x=0, y=0)

trans_stream = Text(root, width=20, height=15, fg='red', bg='black', 
                    font=('helvetica',30), highlightthickness=0, borderwidth=0)
trans_stream.place(x=1500, y=0)

update_avail = Text(root, width=20, height=10, fg='red', bg='black', 
                    font=('helvetica',12), highlightthickness=0, borderwidth=0)
update_avail.place(x=1500, y=120)

# 缓存更新状态,避免频繁调用API
update_status = "Server up to date"

def check_update():
    """后台线程检查更新状态,避免阻塞UI"""
    global update_status
    while True:
        try:
            if plex.isLatest():
                update_status = "Server up to date"
            else:
                update_status = "Server not up to date"
        except Exception as e:
            update_status = f"Check update failed: {str(e)}"
        time.sleep(60)  # 每分钟检查一次更新,减少网络请求

def update():
    # 更新当前流信息
    textv.delete("1.0", END)
    active = []
    for session in plex.sessions():
        active.append("{}   |   {}".format(session.title, str(session.usernames)[2:-2]))
    for n in active:
        textv.insert(END, n + '\n')
    
    # 更新转码流数量
    trans_stream.delete("1.0", END)
    trans_count = len(plex.transcodeSessions())
    trans_stream.insert(END, 'Transcode Streams: ' + str(trans_count))
    
    # 更新状态信息
    update_avail.delete("1.0", END)
    update_avail.insert(END, update_status)
    
    # 定时调用update,保持UI刷新
    root.after(1000, update)

# 启动后台更新检查线程
update_thread = threading.Thread(target=check_update, daemon=True)
update_thread.start()

# 启动UI更新循环
update()
root.mainloop()

关键修改说明

  • 修正Text操作:直接调用delete和insert,不再错误赋值给text属性。
  • 分离更新检查:用后台线程执行plex.isLatest(),避免网络请求阻塞UI线程,同时降低检查频率(改为每分钟一次)。
  • 统一定时器逻辑:将root.after放在update函数末尾,确保每次更新后都会定时触发下一次刷新。
  • 修复插入目标:更新状态文本正确插入到update_avail组件。
  • 移除阻塞循环:删除while True + time.sleep,依赖tkinter主事件循环处理UI刷新。

内容的提问来源于stack exchange,提问作者Cason Berry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 20:45:35