如何判断进程是否在Windows上的Python中响应 [英] How to tell if process is responding in Python on Windows

查看:457
本文介绍了如何判断进程是否在Windows上的Python中响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I can't quite figure out how to do this.

推荐答案

在Windows上,您可以执行以下操作:

On Windows you can do this:

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更可靠:

It is more robust to use the PID though:

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获得更多信息.要直接获得不响应"过程,只需在给定的功能中通过不响应"来更改运行". 在此处查看更多信息.

From tasklist you can get more information than that. To get the "NOT RESPONDING" processes directly, just change "running" by "not responding" in the functions given. See more info here.

这篇关于如何判断进程是否在Windows上的Python中响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆