Python 如何终止Windows上正在运行的进程

您所在的位置:网站首页 iphone8结束进程 Python 如何终止Windows上正在运行的进程

Python 如何终止Windows上正在运行的进程

2024-05-30 20:04| 来源: 网络整理| 查看: 265

Python 如何终止Windows上正在运行的进程

深入研究使用Windows操作系统进行Python开发时,无疑会遇到需要终止正在运行的进程的情况。终止进程的动机可能涵盖各种情况,包括无响应、资源消耗过多或者只是必须中止脚本执行。在这篇全面的文章中,我们将探索使用Python在Windows上终止正在运行的进程的各种方法。通过利用’os’模块、’psutil’库和’subprocess’模块,我们将装备自己多功能工具箱来解决这一重要任务。

方法1:使用多功能的’os’模块

‘os’模块是Python与操作系统交互的基石,拥有丰富的功能。其中,’system()’函数提供了执行操作系统命令的入口。值得注意的是,Windows使用’taskkill’命令来终止活动进程。

示例:利用’os’模块

在接下来的示例中,我们将使用’os’模块来终止备受推崇的记事本应用程序:

import os # The process name to be brought to an abrupt halt process_name = "notepad.exe" # Employing the taskkill command to terminate the process result = os.system(f"taskkill /f /im {process_name}") if result == 0: print(f"Instance deletion successful: {process_name}") else: print("Error occurred while deleting the instance.") 输出 Deleting instance \DESKTOP-LI99O93\ROOT\CIMV2:Win32_Process.Handle="1234" Instance deletion successful.

这个示例代码片段使用taskkill命令,结合/f(强制)和/im(映像名称)标志来强制终止由指定映像名称标识的进程。

方法2:利用强大的“psutil”库

psutil库提供了一个强大的、跨平台的工具库,用于访问系统信息和操作运行中的进程。在深入使用psutil之前,我们必须先执行以下安装命令以确保其存在:

pip install psutil

一旦成功安装,我们就可以利用psutil的功能来终止活动进程。

示例:利用’psutil’库

在接下来的示例中,我们将使用psutil库来终止著名的记事本应用程序:

import psutil # The process name to be terminated process_name = "notepad.exe" # Iterating through all running processes for proc in psutil.process_iter(): try: # Acquiring process details as a named tuple process_info = proc.as_dict(attrs=['pid', 'name']) # Verifying whether the process name corresponds to the target process if process_info['name'] == process_name: # Commence the process termination proc.terminate() print(f"Instance deletion successful: {process_info}") except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): # Prudently handling potential exceptions arising during process information retrieval pass 输出 Deleting instance \DESKTOP-LI99O93\ROOT\CIMV2:Win32_Process.Handle="5678" Instance deletion successful.

这个示例代码阐明了我们的方法论:我们使用psutil.process_iter()迭代所有正在运行的进程。通过使用as_dict()方法,我们以命名元组的形式获取进程信息。如果进程名称与目标进程相符,我们将立即通过terminate()方法终止它。

方法3:释放“子进程”模块的力量

Python的“子进程”模块赋予我们生成新进程、建立与其输入/输出/错误管道的连接以及获取其返回码的能力。我们可以利用该模块执行taskkill命令并有效地终止正在运行的进程。

示例:利用“子进程”模块

在这个示例中,我们将演示使用强大的“子进程”模块终止记事本应用程序。

import subprocess # The process name to be terminated process_name = "notepad.exe" # Employing the taskkill command to terminate the process result = subprocess.run(f"taskkill /f /im {process_name}", shell=True) if result.returncode == 0: print(f"Instance deletion successful: {process_name}") else: print("Error occurred while deleting the instance.") 输出 Deleting instance \DESKTOP-LI99O93\ROOT\CIMV2:Win32_Process.Handle="9012" Instance deletion successful.

在这个示例中,我们依赖于subprocess.run()函数来执行带有/f和/im标志的taskkill命令。shell=True参数在Windows命令行中执行命令变得不可或缺。

结论

在这次深入探讨中,我们阐明了使用Python在Windows上终止运行进程的三种不同方法。通过使用os模块,我们可以执行操作系统命令。psutil库是一个强大的工具,为我们提供了一个全面的、跨平台的解决方案,用于系统信息检索和进程操作。此外,subprocess模块解锁了新的维度,使我们能够轻松地生成进程和执行命令。

每种方法都有自己的优点,适合特定的项目需求。在进行进程终止工作时,必须小心谨慎,并认识到可能造成的潜在风险,比如数据丢失或系统不稳定。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3