在任务管理器中,我只能看到Python/Pythonwin的进程。那么我该如何找出哪个Python脚本正在运行?
在任务管理器中,我只能看到Python/Pythonwin的进程。那么我该如何找出哪个Python脚本正在运行?
通常回答这类问题的方法是使用Process Explorer。您可以在工具提示中看到任何python.exe
或pythonw.exe
实例的完整命令行。
要在Python中获取相同的信息,您可以使用psutil模块。
import psutil
pythons = [[" ".join(p.cmdline), p.pid] for p in psutil.process_iter()
if p.name.lower() in ("python.exe", "pythonw.exe")]
结果pythons
是表示Python进程的列表嵌套列表。每个子列表的第一项是启动该进程的命令行,包括任何选项。第二项是进程ID。
如果你想获得psutil Process
类中的所有内容,可以使用以下代码:
pythons = [p for p in psutil.process_iter() if p.name.lower() in ("python.exe", "pythonw.exe")]
现在,在我的系统上,使用psutil.process_iter()
迭代所有进程需要几秒钟的时间,这对我来说似乎是荒谬的。下面的方法显著更快,因为它在 Python 看到进程之前就进行了进程过滤,但它依赖于 wmic
命令行工具,而不是所有版本的 Windows 都有(尤其是 XP Home 版本缺少该工具)。此处的结果与第一个 psutil
版本相同(每个包含一个 Python 进程的命令行和进程 ID 的列表)。
import subprocess
wmic_cmd = """wmic process where "name='python.exe' or name='pythonw.exe'" get commandline,processid"""
wmic_prc = subprocess.Popen(wmic_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
wmic_out, wmic_err = wmic_prc.communicate()
pythons = [item.rsplit(None, 1) for item in wmic_out.splitlines() if item][1:]
pythons = [[cmdline, int(pid)] for [cmdline, pid] in pythons]
如果wmic
不可用,则会得到一个空列表[]
。由于您知道至少有一个Python进程(即您自己!),因此可以将其捕获为错误并显示适当的消息。win32process.GetCurrentProcessID()
。wmic
来外壳调用。我已经添加了相应的代码。 - kindallwmic
命令行工具外,还使用了psutil
。 - kindallpsutil
方法给了我 "AttributeError: 'function' object has no attribute 'lower'" 错误,但是使用 wmic 选项可以解决。 - craq我在处理kindall的答案时遇到了一些问题。使用Python 3.8:
import psutil
for p in psutil.process_iter():
try:
if p.name().lower() in ["python.exe", "pythonw.exe"]:
print(p.pid, p.cmdline)
except:
continue
使用Python 3:
import psutil
pythons = [[" ".join(p.cmdline()), p.pid] for p in psutil.process_iter()
if p.name().lower() in ["python.exe", "pythonw.exe"]]