San*_*nde 8 python windows process
我想知道如何检查程序是否正在使用python运行,如果没有运行它.我有两个python脚本,一个是GUI,它监视另一个脚本.所以基本上如果第二个脚本由于某种原因崩溃我希望它重新开始.
我正在python 3.4.2Windows上使用.
小智 14
模块psutil可以帮助你.要列出所有进程运行使用:
import psutil
print(psutil.pids()) # Print all pids
Run Code Online (Sandbox Code Playgroud)
要访问流程信息,请使用:
p = psutil.Process(1245) # The pid of desired process
print(p.name()) # If the name is "python.exe" is called by python
print(p.cmdline()) # Is the command line this process has been called with
Run Code Online (Sandbox Code Playgroud)
如果你psutil.pids()在for上使用,你可以验证所有这个进程是否使用python,如:
for pid in psutil.pids():
p = psutil.Process(pid)
if p.name() == "python.exe":
print("Called By Python:"+ str(p.cmdline())
Run Code Online (Sandbox Code Playgroud)
psutil的文档可在以下网址获得:https://pypi.python.org/pypi/psutil
假设脚本的名称是Pinger.py,您可以使用此功能
def verification():
for pid in psutil.pids():
p = psutil.Process(pid)
if p.name() == "python.exe" and len(p.cmdline()) > 1 and "Pinger.py" in p.cmdline()[1]:
print ("running")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27441 次 |
| 最近记录: |