【问题标题】:Python GUI Multiprocessing and still freezingPython GUI多处理并且仍然冻结
【发布时间】:2014-12-22 20:49:05
【问题描述】:

我正在使用 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()

【问题讨论】:

    标签: python multithreading python-2.7 multiprocessing


    【解决方案1】:
    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)
    

    【讨论】:

    • 我认为这绝对是当前问题的解决方案,尽管下一个问题将是那些“全局”变量不会跨进程全局...
    【解决方案2】:

    根据 Kevin 的回答,您在创建每个流程实例时都会调用这些函数。所以它们实际上都在主进程中运行。

    但是,一旦您解决了该问题,您的“全局”变量将无法按预期工作。当一个进程被创建时,它需要一个父进程内存的副本。对该内存的任何更改都不会在进程之间共享。

    要获得您想要的结果,您必须使用 Python 的线程库。不是多进程库。

    线程与父进程共享相同的内存空间。这可能导致其自身的问题。尽管在您的情况下,您要更改的全局变量只是整数常量,所以应该没问题。

    from threading import Thread
    
    data_funcs = (
        get_uptime, 
        get_cpu, 
        get_mem_mbytes, 
        get_mem_pct, 
        draw_window
    )
    
    threads = [Thread(target=f) for f in data_funcs]
    
    for t in threads:
        t.start()
    

    是您应该使用的一般模式。然后,您必须找出一种在关闭主进程时杀死这些线程的方法,否则它将挂起。

    【讨论】:

    • 实际上,通读代码还有其他问题,这是如何工作的,这些线程只会运行一次并退出,因为它们不包含任何循环,你必须在复习器中创建并运行这些线程。
    猜你喜欢
    • 2018-11-08
    • 2016-06-13
    • 2021-09-30
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2019-08-14
    相关资源
    最近更新 更多