是否可以从 Python 中杀死 Windows 上的进程? [英] Is it possible to kill a process on Windows from within Python?

查看:36
本文介绍了是否可以从 Python 中杀死 Windows 上的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Python 2.6.有时,某个进程打开了多个实例,而该进程本身会导致一些问题.我希望能够以编程方式检测到该进程的多个实例并杀死它们.

I'm using Python 2.6. Sometimes there become several instances of a certain process open, and that process causes some problems in itself. I want to be able to programatically detect that there are multiple instances of that process and to kill them.

例如,可能在某些情况下有 50 个 make.exe 实例打开.我希望能够告诉有 20 个实例打开,并将它们全部杀死.这是如何实现的?

For example, maybe in some cases there are 50 instances of make.exe open. I want to be able to tell that there are 20 instances open, and to kill them all. How is this accomplished?

推荐答案

我认为你可以使用 taskkill 和 Python os.system()

I would think you could just use taskkill and the Python os.system()

import os
os.system("taskkill /im make.exe")

<小时>

注意:我只是注意到您可能必须完全限定 taskkill 路径.我使用的是 Linux 机器,所以我无法测试...


Note: I would just note you might have to fully qualify the taskkill path. I am using a Linux box so I can't test...

这篇关于是否可以从 Python 中杀死 Windows 上的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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