How to tell if process is responding in Python on Windows

2024/9/19 9:17:30

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.

Answer

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 Trueelse:return False

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 Trueelse:return False

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.

https://en.xdnf.cn/q/72603.html

Related Q&A

How to load and use a pretained PyTorch InceptionV3 model to classify an image

I have the same problem as How can I load and use a PyTorch (.pth.tar) model which does not have an accepted answer or one I can figure out how to follow the advice given.Im new to PyTorch. I am trying…

Append list to pandas DataFrame as new row with index

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as la…

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that Ive captured wit…

Saving matplotlib subplot figure to image file

Im fairly new to matplotlib and am limping along. That said, I havent found an obvious answer to this question.I have a scatter plot I wanted colored by groups, and it looked like plotting via a loop w…

Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

I have a 3 dimensional data set that I am trying to manipulate in the following way. data.shape = (643, 2890, 10) vector.shape = (643,)I would like numpy to see data as a 643 length 1-D array of 2890x1…

delete node in binary search tree python

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.inser…

ImportError: cannot import name cbook when using PyCharms Profiler

I am trying to run the PyCharm profiler but I get the following error message:Traceback (most recent call last):File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py&…

Replicating SAS first and last functionality with Python

I have recently migrated to Python as my primary tool for analysis and I am looking to be able to replicate the first. & last. functionality found in SAS. The SAS code would be as follows;data data…

Can mypy track string literals?

Is there anyway to make this work from typing import Literal def foo(bar: Literal["bar"]) -> Literal["foo"]:foo = "foo"return foobar = "bar" foo(bar)Here are …

Lazy loading of attributes

How would you implement lazy load of object attributes, i.e. if attributes are accessed but dont exist yet, some object method is called which is supposed to load these?My first attempt isdef lazyload…