如何知道Windows中的进程是否在python中运行

时间:2014-03-12 12:43:19

标签: python process taskmanager

我使用的是python 2.7和Windows 7 64位。我想知道在任务管理器/进程中是否正在运行进程(python.exe)。 我经历了http://www.videntity.com/2010/05/check-to-make-sure-a-process-is-running-and-restart-it-if-its-not-a-recipe-in-python/,但它不适用于Windows。

3 个答案:

答案 0 :(得分:3)

您链接的页面使用os.popen()(official docs here

在windows中,你应该使用“tasklist”作为os.popen()的arg,而不是“ps -Af”

e.g。

>>> import os
>>> tmp = os.popen("tasklist").read()  # it would return a str type
>>> "python.exe" in tmp
True

答案 1 :(得分:3)

以下是我使用win32

的方式
from win32com.client import GetObject
WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')

if "python.exe" in [process.Properties_('Name').Value for process in processes]:
    #do the thing

答案 2 :(得分:0)

您应该能够在任务管理器的进程选项卡的后台进程中看到您的进程,名称为pythonw.exe(64位)