系统相关
首页 > 系统相关> > Python进程psutil库

Python进程psutil库

作者:互联网

闲着无聊写个关于进程的东西 参考一下

import psutil, os

def processlist():  #遍历进程列表
    plist = []
    for p in psutil.process_iter():
        try:
            pinfo = p.as_dict(attrs = ['pid', 'name'])
        except psutil.NoSuchProcess:
            pass
        else:
            plist.append(pinfo)
    return plist

def processcheck(): #检查某个进程是否存在
    plist = processlist()
    for i in plist:
        #print(i['name'])   #调试时使用 遍历进程名字
        if process_name == i['name']:
            return i['pid']
    else:
        return False

def processkill():  #结束某个进程
    pid = processcheck()
    if pid == False:
        print("该进程不存在")
    else:
        os.popen("taskkill.exe /F /pid:" + str(pid))

if __name__ == '__main__':
    process_name = "QtScrcpy.exe"
    print(processlist())

 

标签:__,name,Python,pid,psutil,进程,plist
来源: https://www.cnblogs.com/ruler-madman/p/14441927.html