简体   繁体   中英

Python: Kill a tensorflow subprocess

Is it possible to kill a process of another user with python by using:

import subprocess

def killProcess(pid):
    p = subprocess.Popen(['sudo','kill','-9',str(pid)], stdout=subprocess.PIPE)

Because if I execute this, nothing happens. If I execute sudo kill -9 pid in terminal no matter which user Iam logged in it workds. So I think there is something wrong with my Popen execution. I try to kill subprocesses spawned with pythons multiprocessing module. Each of those subprocesses creates tensorflow instances. When the main process has killed the subprocesses still blocking the GPUs memory and therefore has to be killed.

I also tried the psutil.Process(pid).terminate() approach. But then I get the error message:

AccessDenied: psutil.AccessDenied (pid=326080)

Anyone has an idea?

Best regards!

try using psutil,

for i in psutil.process_iter():
   if 'tensorflow' in i.name():
       i.kill()

or

[i.kill() for i in psutil.process_iter() if 'tensorflow' in i.name()]

Each process iter having their own .kill() attribute.

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