我查看了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的任何有用方法?
提前致谢!
答案 0 :(得分:0)
>>> 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上。