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

后端 未结 8 1069
后悔当初
后悔当初 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:31
    import psutil
    
    process = filter(lambda p: p.name() == "YourProcess.exe", psutil.process_iter())
    for i in process:
      print i.name,i.pid
    

    Give all pids of "YourProcess.exe"

    0 讨论(0)
  • 2020-12-02 10:38

    I don't think you will be able to find a purely python-based, portable solution without using /proc or command line utilities, at least not in python itself. Parsing os.system is not ugly - someone has to deal with the multiple platforms, be it you or someone else. Implementing it for the OS you are interested in should be fairly easy, honestly.

    0 讨论(0)
提交回复
热议问题