Cross-platform way to get PIDs by process name in python

后端 未结 8 1089
后悔当初
后悔当初 2020-12-02 09:35

Several processes with the same name are running on host. What is the cross-platform way to get PIDs of those processes by name using python or jyth

8条回答
  •  长情又很酷
    2020-12-02 10:30

    There's no single cross-platform API, you'll have to check for OS. For posix based use /proc. For Windows use following code to get list of all pids with coresponding process names

    from win32com.client import GetObject
    WMI = GetObject('winmgmts:')
    processes = WMI.InstancesOf('Win32_Process')
    process_list = [(p.Properties_("ProcessID").Value, p.Properties_("Name").Value) for p in processes]
    

    You can then easily filter out processes you need. For more info on available properties of Win32_Process check out Win32_Process Class

提交回复
热议问题