简体   繁体   中英

psutil module in python

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 (eg two instances of VLC media player), does psutil.kill() kills all the instances of that process or only one of the instances?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM