启动时获取Python进程自己的PID

时间:2019-12-06 19:40:24

标签: python

我正在用Python创建一个桌面应用程序管理器程序。这是用tkinter制作的GUI程序。我通过此命令获得正在运行的应用程序的列表:

subprocess.Popen(["powershell.exe", "Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path"], stdout=subprocess.PIPE)

它在程序启动时自动运行,但未显示运行该程序的python进程。每当我通过手动运行该命令刷新应用程序列表之后,Python进程也会显示出来。

基本上我运行的代码是这样:

import tkinter as tk

class Gui:

    def __init__(self):
        self.refresh()

    def refresh(self):
        p = subprocess.Popen(["powershell.exe", "Get-Process | Where-Object { $_.MainWindowTitle } | Format-List Id,Name,Path"], stdout=subprocess.PIPE)

        for line in io(p.stdout, encoding='utf-8').readlines():
            line = " ".join(line.split())
            print(line)

root = tk.Tk()
gui = Gui()
root.mainloop()

我不希望进行自动刷新,因为它很繁重并且使程序的响应速度较慢。在调用mainloop来获取列出的Python进程的PID之后,还有什么方法可以运行刷新功能?

0 个答案:

没有答案