我正在寻找一个带有python的Ubuntu进程的预打印。我正在了解psutil,但是你知道有没有更好的吗?

2024-04-20 08:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我查看了psutil库(https://code.google.com/archive/p/psutil/wikis/Documentation.wiki),得到了一些有用的东西,比如

````

PROCESS_ATTRS = ['username', 'cpu_num', 'num_ctx_switches', 'pid', 'memory_full_info', 'connections', 'cmdline', 'create_time', 'ionice', 'num_fds', 'memory_maps', 'cpu_percent', 'terminal', 'ppid', 'cwd', 'nice', 'status', 'cpu_times', 'io_counters', 'memory_info', 'threads', 'open_files', 'uids', 'num_threads', 'exe', 'name', 'gids', 'cpu_affinity', 'memory_percent', 'environ']
for proc in psutil.process_iter():
    print("Querying process: %s [%s]" % (proc.name(), proc.pid))
    print proc.as_dict(attrs=PROCESS_ATTRS)

````

有没有其他的库,或者我缺少的关于psutil的有用方法?你知道吗

提前谢谢!你知道吗


Tags: namehttpsinfoproccpuprocesspidnum
1条回答
网友
1楼 · 发布于 2024-04-20 08:36:40
>>> import psutil
>>> from pprint import pprint as pp
>>> pp([p.info for p in psutil.process_iter(attrs=('pid', 'name'))])
[{'name': 'systemd', 'pid': 1},
 {'name': 'kthreadd', 'pid': 2},
 {'name': 'ksoftirqd/0', 'pid': 3},
 {'name': 'kworker/0:0H', 'pid': 5},
 {'name': 'rcu_sched', 'pid': 7},
 {'name': 'rcu_bh', 'pid': 8},
 {'name': 'migration/0', 'pid': 9},
 {'name': 'watchdog/0', 'pid': 10},
 {'name': 'watchdog/1', 'pid': 11},
 {'name': 'migration/1', 'pid': 12},
 {'name': 'ksoftirqd/1', 'pid': 13},
 ...
]

更多示例:http://psutil.readthedocs.io/en/latest/#filtering-and-sorting-processes

注意:您使用的文档是古代的。psutil现在托管在github上。你知道吗

相关问题 更多 >