From 467821da84d0e71dc4f19a9915a3cba34b68a1ae Mon Sep 17 00:00:00 2001 From: cetteup Date: Wed, 23 Nov 2022 22:45:38 +0100 Subject: [PATCH] feat: Use psutil to check process status and kill processes --- BF2AutoSpectator/common/utility.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/BF2AutoSpectator/common/utility.py b/BF2AutoSpectator/common/utility.py index a95d726..e816618 100644 --- a/BF2AutoSpectator/common/utility.py +++ b/BF2AutoSpectator/common/utility.py @@ -95,15 +95,28 @@ def list_filter_zeroes(list_with_zeroes: list) -> list: def is_responding_pid(pid: int) -> bool: """Check if a program (based on its PID) is responding""" - cmd = 'tasklist /FI "PID eq %d" /FI "STATUS eq running"' % pid - status = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() - return str(pid) in str(status) + proc = get_proc_by_pid(pid) + + if proc is None: + return False + + try: + return proc.is_running() + except (psutil.NoSuchProcess, psutil.AccessDenied): + return False def taskkill_pid(pid: int) -> bool: - cmd = 'taskkill /F /PID %d' % pid - output = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout.read() - return 'has been terminated' in str(output) + proc = get_proc_by_pid(pid) + + if proc is None: + return False + + try: + proc.kill() + return proc.is_running() + except (psutil.NoSuchProcess, psutil.AccessDenied): + return False def press_key(key_code: int) -> None: