psutil module in python

674 views Asked by At

I am a newbie in python and trying to get an understanding of psutil module. My question is that if have more than 1 instance of a process (e.g. two instances of VLC media player), does psutil.kill() kills all the instances of that process or only one of the instances?

1

There are 1 answers

0
cdarke On BEST ANSWER

No. kill is a method called on a process object, so it is a question of finding the right process. You might iterate through them:

for proc in psutil.process_iter():
try:
    print("{:4d} {:4d} {:s}".
          format(proc.pid, proc.ppid, proc.exe))
except psutil.AccessDenied:
    pass
except psutil.NoSuchProcess as err:
    print("****",err) 

The example exception handling is to handle race conditions - the process might finish between finding it and interrogating it. The issue is identifying the correct process you wish to kill. Typically you would use the process ID, pid, or maybe cmdline. If you do wish to kill all instances, then use exe.