Skip to content

Commit

Permalink
feat: Use psutil to check process status and kill processes
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Nov 23, 2022
1 parent c7166ae commit 467821d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions BF2AutoSpectator/common/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 467821d

Please sign in to comment.