使用python subprocess.call杀死所有正在运行的python文件 [英] using python subprocess.call to kill all running python files

查看:417
本文介绍了使用python subprocess.call杀死所有正在运行的python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试(按需)杀死当前正在运行的所有python进程.
我正在使用以下命令:

I'm trying to kill (on a demand) all the python processes that are running at the moment.
I was using this command:

from subprocess import call  
call('pkill python', shell=True)  
print 'Killed them all!'

但是,当然-我的程序也是python程序,所以最终,它在调用'call'后没有到达打印行.

But, of course - my program is also a python program, so eventually, it doesn't reach the print line after calling 'call'.

我该怎么办才能避免我的程序同时杀死自己,同时杀死所有其他python进程?
谢谢.

What can I do in order to avoid my program to kill also itself, while killing all other python processes?
Thanks.

推荐答案

您可能想尝试跨平台 psutil 库:

You may want to try cross-platform psutil library:

import os
import psutil

mypid = os.getpid()
for proc in psutil.process_iter():
    if proc.name == 'python' and proc.pid != mypid:
        proc.kill()

这篇关于使用python subprocess.call杀死所有正在运行的python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆