gpt4 book ai didi

python - 如何判断进程是否在 Windows 上的 Python 中响应

转载 作者:行者123 更新时间:2023-11-28 16:43:29 25 4
gpt4 key购买 nike

我正在编写一个 python 脚本来保持一个有问题的程序打开,我需要弄清楚该程序是否没有响应并在 Windows 上将其关闭。我不太清楚该怎么做。

最佳答案

在 Windows 上你可以这样做:

import os
def isresponding(name):
os.system('tasklist /FI "IMAGENAME eq %s" /FI "STATUS eq running" > tmp.txt' % name)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
if a[-1].split()[0] == name:
return True
else:
return False

不过使用 PID 更稳健:

def isrespondingPID(PID):
os.system('tasklist /FI "PID eq %d" /FI "STATUS eq running" > tmp.txt' % PID)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
if int(a[-1].split()[1]) == PID:
return True
else:
return False

tasklist 中你可以获得比这更多的信息。要直接得到“NOT RESPONDING”进程,只需在给出的函数中将“running”改为“not responding”即可。 See more info here .

关于python - 如何判断进程是否在 Windows 上的 Python 中响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com