简体   繁体   English

Python:杀死tensorflow子进程

[英]Python: Kill a tensorflow subprocess

Is it possible to kill a process of another user with python by using: 是否有可能使用python杀死另一个用户的进程:

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. 如果我在终端中执行sudo kill -9 pid,无论Iam登录到哪个用户,它都将工作。 So I think there is something wrong with my Popen execution. 所以我认为我的Popen执行有问题。 I try to kill subprocesses spawned with pythons multiprocessing module. 我试图杀死由pythons multiprocessing模块产生的子进程。 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. 当主进程终止后,子进程仍然阻塞GPU内存,因此必须终止。

I also tried the psutil.Process(pid).terminate() approach. 我还尝试了psutil.Process(pid).terminate()方法。 But then I get the error message: 但是然后我收到错误消息:

AccessDenied: psutil.AccessDenied (pid=326080)

Anyone has an idea? 有人有主意吗?

Best regards! 最好的祝福!

try using psutil, 尝试使用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. 每个进程都有自己的.kill()属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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