2017-09-08 1 views
0

Ich habe über die psutil Bibliothek sah (https://code.google.com/archive/p/psutil/wikis/Documentation.wiki) und bekam ein paar nützliche Dinge wieIch suche nach einem Pretty-Print eines Ubuntu-Prozesses mit Python. Ich lerne psutil kennen, aber keine Ahnung, ob es etwas Besseres gibt?

`` `

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) 

` ``

Gibt es eine andere Bibliothek für diese oder eine beliebige nützliche Methoden über psutil, die ich vermisse?

Vielen Dank im Voraus!

Antwort

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}, 
... 
] 

Weitere Beispiele hier: http://psutil.readthedocs.io/en/latest/#filtering-and-sorting-processes

Hinweis: das Dokument, das Sie verwenden ist alten. psutil wird jetzt auf github gehostet.

Verwandte Themen