简体   繁体   中英

How to know whether a process in windows is running or not in python

I am using python 2.7 and windows 7 64 bit. I want to know whether a process(python.exe) is running or not in task manager/Processes. I had gone through http://www.videntity.com/2010/05/check-to-make-sure-a-process-is-running-and-restart-it-if-its-not-a-recipe-in-python/ , but it is not for windows.

The page you linked uses os.popen()( official docs here )

In windows, you should use "tasklist" as arg for os.popen(), rather than "ps -Af"

eg

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

Here's how I do it with 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM