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

2024-10-01 13:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python2.7和windows7 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的。在


Tags: tocomhttp管理器make进程ischeck
3条回答

您应该能够在“任务管理器”的“进程”选项卡的后台进程中看到具有以下名称的流程pythonw.exe(64位)

以下是我如何使用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

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

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

例如

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

相关问题 更多 >