1. Description
Windows provides some API functions that allow processes to query the list of running processes in the system and information related to them. Using these API functions, a process can enumerate the processes in the system and obtain some limited information such as process ID, parent process ID, process priority, etc. However, the information does not include the names or specific details of other processes.
To implement reading the names of other processes in Python, you can use the psutil library.
psutil is a cross-platform library that provides many functions related to system processes and system resources.
Two, Python code implementation
import psutil
def get_running_processes():
processes = []
for proc in psutil.process_iter(['pid', 'name']):
try:
# 获取进程名称
process_name = proc.info['name']
processes.append(process_name)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return processes
# 获取正在运行的进程名称列表
running_processes = get_running_processes()
# 打印进程名称列表
for process in running_processes:
print(process)
3. Running results
This code iterates over all running processes using the psutil.process_iter() function and gets the name of each process.