gpt4 book ai didi

Python GUI 多处理并且仍然卡住

转载 作者:行者123 更新时间:2023-11-30 23:16:42 31 4
gpt4 key购买 nike

我正在使用 TKinter 为我正在制作的 python 程序绘制 GUI,并且它的更新时间约为 200 毫秒,但是当程序查询数据时,它会锁定程序,因为获取数据需要一秒钟的时间。我尝试将其写入多处理中,这样每个查询都是它自己的进程,并且只与全局变量共享信息,因为我的程序是一个使用 wmi 获取性能数据的实时程序。至少这是我到目前为止所拥有的。不是最终目标,只是开始。因此,如果您能帮助我弄清楚为什么即使使用多重处理,如果当我在屏幕上拖动应用程序时查询信息,它也会卡住一秒钟。

import wmi
import time
import Tkinter as tk
from multiprocessing import cpu_count
import Image
from PIL import ImageTk
from Tkinter import Button, Label
import threading
from multiprocessing import Process, Value, Array

window = Tk();
global pct_in_use
global available_mbytes
global utilization
global hours_up
a= 0
b=0


def build_labels(gui, string):
var = StringVar()
label = Label( gui, textvariable=var, relief=RAISED )
var.set(string)
return label

def get_uptime():
global hours_up
c = wmi.WMI()
secs_up = int([uptime.SystemUpTime for uptime in c.Win32_PerfFormattedData_PerfOS_System()][0])
hours_up = secs_up / 3600
return hours_up

def get_cpu():
global utilization
c = wmi.WMI()
utilizations = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
utilization = int(sum(utilizations) / len(utilizations)) # avg all cores/processors
return utilization

def get_mem_mbytes():
global available_mbytes
c = wmi.WMI()
available_mbytes = int([mem.AvailableMBytes for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return available_mbytes

def get_mem_pct():
global pct_in_use
c = wmi.WMI()
pct_in_use = int([mem.PercentCommittedBytesInUse for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return pct_in_use

def Draw():
global mem_per_lb
global cpu_lb
global up_time_lb
global mb_used_lb

mem_pct = 0
mem_per_lb = tk.Label(text='Memory % ' + str(mem_pct))
mem_per_lb.place(x=10, y=10)

cpu = 0
cpu_lb = tk.Label(text='CPU % ' + str(cpu))
cpu_lb.place(x=10, y=30)

mem_pct = 0
up_time_lb = tk.Label(text='UP Time % ' + str(mem_pct))
up_time_lb.place(x=10, y=50)

mem_pct = 0
mb_used_lb = tk.Label(text='Memory MB ' + str(mem_pct))
mb_used_lb.place(x=10, y=70)


def Refresher():
global mem_per_lb
global cpu_lb
global up_time_lb
global mb_used_lb

mem_pct = get_mem_pct()
cpu = get_cpu()
up_time = get_uptime()
mbused = get_mem_mbytes()

window.wm_title('Vision' + time.asctime())
mem_per_lb.configure(text='Memory % ' + str(pct_in_use))
cpu_lb.configure(text='CPU ' + str(utilization))
up_time_lb.configure(text='UP Time ' + str(hours_up))
mb_used_lb.configure(text='Memory MB ' + str(available_mbytes))

window.after(200, Refresher) # every second...

def draw_window(): #creates a window
window.geometry('704x528+100+100')

image = Image.open('bg.jpg') #gets image (also changes image size)
image = image.resize((704, 528))
imageFinal = ImageTk.PhotoImage(image)
label = Label(window, image = imageFinal) #creates label for image on window
label.pack()
label.place(x = a, y = b) #sets location of label/image using variables 'a' and 'b'

Draw()
Refresher()
window.mainloop()

up_time_p = Process(target=get_uptime())
cpu_p = Process(target=get_cpu())
mb_p = Process(target=get_mem_mbytes())
pct_p = Process(target=get_mem_pct())
win_p = Process(target=draw_window())

up_time_p.start()
mb_p.start()
pct_p.start()
cpu_p.start()
win_p.start()

最佳答案

up_time_p = Process(target=get_uptime())
cpu_p = Process(target=get_cpu())
mb_p = Process(target=get_mem_mbytes())
pct_p = Process(target=get_mem_pct())
win_p = Process(target=draw_window())

我认为在向流程提供目标时不应该包含括号。如果这样做,这些函数将在主线程中执行,并且这些函数返回的任何内容都将成为目标。

up_time_p = Process(target=get_uptime)
cpu_p = Process(target=get_cpu)
mb_p = Process(target=get_mem_mbytes)
pct_p = Process(target=get_mem_pct)
win_p = Process(target=draw_window)

关于Python GUI 多处理并且仍然卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610222/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com