1

I've been inspired to try to make a wordcloud wallpaper generator by a similar github project. I'm not much experienced and this seems like a good beginner-ish project.

For now I'm trying to get some usable output out of psutil and it does seem to work but something is just not right.

import wordcloud # For wordcloud generation
import psutil # for the linux process data and cpu usage

#Creates a list to store the output of procInfo
theOneProcessList = list()

# Lists every process and it's cpu usage and appends it to theOneProcessList
for proc in psutil.process_iter(attrs=None, ad_value=None):
    procInfo = proc.as_dict(attrs=['name', 'cpu_percent'])
    theOneProcessList.append(procInfo)
print(theOneProcessList)

The output ends up being an array that lists every process in linux and their corresponding cpu usage. Something like this:

[{'name': 'systemd', 'cpu_percent': 0.0}, {'name': 'kthreadd', 'cpu_percent': 0.0}, {'name': 'rcu_gp', 'cpu_percent': 0.0}, {'name': 'rcu_par_gp', 'cpu_percent': 0.0}, {'name': 'kworker/0:0H-kblockd', 'cpu_percent': 0.0}, {'name': 'mm_percpu_wq', 'cpu_percent': 0.0}, {'name': 'ksoftirqd/0', 'cpu_percent': 0.0}, {'name': 'rcu_preempt', 'cpu_percent': 0.0}, {'name': 'rcu_sched', 'cpu_percent': 0.0}, {'name': 'rcu_bh', 'cpu_percent': 0.0}, {'name': 'rcuc/0', 'cpu_percent': 0.0}, {'name': 'rcub/0', 'cpu_percent': 0.0}, {'name': 'migration/0', 'cpu_percent': 0.0}, {'name': 'idle_inject/0', 'cpu_percent': 0.0}, {'name': 'cpuhp/0', 'cpu_percent': 0.0}, {'name': 'cpuhp/1', 'cpu_percent': 0.0}, {'name': 'idle_inject/1', 'cpu_percent': 0.0}, {'name': 'migration/1', 'cpu_percent': 0.0}, {'name': 'rcuc/1', 'cpu_percent': 0.0}, {'name': 'ksoftirqd/1', 'cpu_percent': 0.0}, {'name': 'kworker/1:0H-kblockd', 'cpu_percent': 0.0}, {'name': 'cpuhp/2', 'cpu_percent': 0.0}, {'name': 'idle_inject/2', 'cpu_percent': 0.0}, {'name': 'migration/2', 'cpu_percent': 0.0}, {'name': 'rcuc/2', 'cpu_percent': 0.0}, {'name': 'ksoftirqd/2', 'cpu_percent': 0.0}, {'name': 'kworker/2:0H-kblockd', 'cpu_percent': 0.0}, {'name': 'cpuhp/3', 'cpu_percent': 0.0}, {'name': 'idle_inject/3', 'cpu_percent': 0.0}, {'name': 'migration/3', 'cpu_percent': 0.0}, {'name': 'rcuc/3', 'cpu_percent': 0.0}, {'name': 'ksoftirqd/3', 'cpu_percent': 0.0}, {'name': 'kworker/3:0H-kblockd', 'cpu_percent': 0.0}, {'name': 'kdevtmpfs', 'cpu_percent': 0.0}, {'name': 'netns', 'cpu_percent': 0.0}, {'name': 'rcu_tasks_kthre', 'cpu_percent': 0.0}, {'name': 'kauditd', 'cpu_percent': 0.0}, {'name': 'kworker/2:1-mm_percpu_wq', 'cpu_percent': 0.0}, {'name': 'kworker/3:1-events', 'cpu_percent': 0.0}, {'name': 'khungtaskd', 'cpu_percent': 0.0}, {'name': 'oom_reaper', 'cpu_percent': 0.0}, {'name': 'writeback', 'cpu_percent': 0.0}, {'name': 'kcompactd0', 'cpu_percent': 0.0}, {'name': 'ksmd', 'cpu_percent': 0.0}, {'name': 'khugepaged', 'cpu_percent': 0.0}, {'name': 'crypto', 'cpu_percent': 0.0}, {'name': 'kintegrityd', 'cpu_percent': 0.0}, {'name': 'kblockd', 'cpu_percent': 0.0}, {'name': 'edac-poller', 'cpu_percent': 0.0}, {'name': 'devfreq_wq', 'cpu_percent': 0.0}, {'name': 'watchdogd', 'cpu_percent': 0.0}, {'name': 'kswapd0', 'cpu_percent': 0.0}, {'name': 'kthrotld', 'cpu_percent': 0.0}, {'name': 'irq/42-pciehp', 'cpu_percent': 0.0}, {'name': 'acpi_thermal_pm', 'cpu_percent': 0.0}, {'name': 'nvme-wq', 'cpu_percent': 0.0}, {'name': 'nvme-reset-wq', 'cpu_percent': 0.0}, {'name': 'nvme-delete-wq', 'cpu_percent': 0.0}, {'name': 'ipv6_addrconf', 'cpu_percent': 0.0}, {'name': 'kstrp', 'cpu_percent': 0.0}, {'name': 'charger_manager', 'cpu_percent': 0.0}, {'name': 'ata_sff', 'cpu_percent': 0.0}, {'name': 'scsi_eh_0', 'cpu_percent': 0.0}, {'name': 'scsi_tmf_0', 'cpu_percent': 0.0}, {'name': 'scsi_eh_1', 'cpu_percent': 0.0}, {'name': 'scsi_tmf_1', 'cpu_percent': 0.0}, {'name': 'kworker/1:1H-kblockd', 'cpu_percent': 0.0}, {'name': 'kworker/0:1H-kblockd', 'cpu_percent': 0.0}, {'name': 'kworker/2:1H-kblockd', 'cpu_percent': 0.0}, {'name': 'kworker/u9:0-hci0', 'cpu_percent': 0.0}, {'name': 'jbd2/sda6-8', 'cpu_percent': 0.0}, {'name': 'ext4-rsv-conver', 'cpu_percent': 0.0}, {'name': 'kworker/3:1H-kblockd', 'cpu_percent': 0.0}, {'name': 'systemd-journald', 'cpu_percent': 0.0}, {'name': 'lvmetad', 'cpu_percent': 0.0}, {'name': 'iprt-VBoxWQueue', 'cpu_percent': 0.0}, {'name': 'iprt-VBoxTscThr', 'cpu_percent': 0.0}, {'name': 'systemd-udevd', 'cpu_percent': 0.0}, {'name': 'irq/49-mei_me', 'cpu_percent': 0.0}, {'name': 'cfg80211', 'cpu_percent': 0.0}, {'name': 'kmemstick', 'cpu_percent': 0.0}, {'name': 'rtsx_usb_ms_1', 'cpu_percent': 0.0}, {'name': 'i915/signal:0', 'cpu_percent': 0.0}, {'name': 'i915/signal:1', 'cpu_percent': 0.0}, {'name': 'i915/signal:2', 'cpu_percent': 0.0}, {'name': 'i915/signal:6', 'cpu_percent': 0.0}, {'name': 'kworker/u9:1-hci0', 'cpu_percent': 0.0}, {'name': 'nv_queue', 'cpu_percent': 0.0}, {'name': 'nv_queue', 'cpu_percent': 0.0}, {'name': 'nvidia-modeset/', 'cpu_percent': 0.0}, {'name': 'nvidia-modeset/', 'cpu_percent': 0.0}, {'name': 'irq/53-nvidia', 'cpu_percent': 0.0}, {'name': 'nvidia', 'cpu_percent': 0.0}, {'name': 'nv_queue', 'cpu_percent': 0.0}, {'name': 'systemd-timesyncd', 'cpu_percent': 0.0}, {'name': 'bluetoothd', 'cpu_percent': 0.0}, {'name': 'dbus-daemon', 'cpu_percent': 0.0}, {'name': 'systemd-logind', 'cpu_percent': 0.0}, {'name': 'avahi-daemon', 'cpu_percent': 0.0}, {'name': 'crond', 'cpu_percent': 0.0}, {'name': 'ModemManager', 'cpu_percent': 0.0}, {'name': 'NetworkManager', 'cpu_percent': 0.0}, {'name': 'avahi-daemon', 'cpu_percent': 0.0}, {'name': 'sddm', 'cpu_percent': 0.0}, {'name': 'polkitd', 'cpu_percent': 0.0}, {'name': 'Xorg', 'cpu_percent': 0.0}, {'name': 'wpa_supplicant', 'cpu_percent': 0.0}, {'name': 'udisksd', 'cpu_percent': 0.0}, {'name': 'upowerd', 'cpu_percent': 0.0}, {'name': 'sddm-helper', 'cpu_percent': 0.0}, {'name': 'systemd', 'cpu_percent': 0.0}, {'name': '(sd-pam)', 'cpu_percent': 0.0}, {'name': 'appimagelauncherfs', 'cpu_percent': 0.0}, {'name': 'appimagelauncherd', 'cpu_percent': 0.0}, {'name': 'kwalletd5', 'cpu_percent': 0.0}, {'name': 'startkde', 'cpu_percent': 0.0}, {'name': 'dbus-daemon', 'cpu_percent': 0.0}, {'name': 'start_kdeinit', 'cpu_percent': 0.0}, {'name': 'kdeinit5', 'cpu_percent': 0.0}, {'name': 'klauncher', 'cpu_percent': 0.0}, {'name': 'kded5', 'cpu_percent': 0.0}, {'name': 'kaccess', 'cpu_percent': 0.0}, {'name': 'kwrapper5', 'cpu_percent': 0.0}, {'name': 'ksmserver', 'cpu_percent': 0.0}, {'name': 'kglobalaccel5', 'cpu_percent': 0.0}, {'name': 'dconf-service', 'cpu_percent': 0.0}, {'name': 'kwin_x11', 'cpu_percent': 0.0}, {'name': 'baloo_file', 'cpu_percent': 0.0}, {'name': 'krunner', 'cpu_percent': 0.0}, {'name': 'plasmashell', 'cpu_percent': 0.0}, {'name': 'polkit-kde-authentication-agent-1', 'cpu_percent': 0.0}, {'name': 'xembedsniproxy', 'cpu_percent': 0.0}, {'name': 'kactivitymanagerd', 'cpu_percent': 0.0}, {'name': 'gmenudbusmenuproxy', 'cpu_percent': 0.0}, {'name': 'kdeconnectd', 'cpu_percent': 0.0}, {'name': 'msm_kde_notifier', 'cpu_percent': 0.0}, {'name': 'octopi-notifier', 'cpu_percent': 0.0}, {'name': 'pulseaudio', 'cpu_percent': 0.0}, {'name': 'rtkit-daemon', 'cpu_percent': 0.0}, {'name': 'AppRun', 'cpu_percent': 0.0}, {'name': 'python3', 'cpu_percent': 0.0}]

But for some reason the cpu usage is always 0 for every process. That just won't work for a wordcloud. Their size difference varies by the cpu usage value.

Am I doing something wrong? Is there an alternate way to do it that actually works right?

Iverol
  • 39
  • 7
  • I think you have to call it multiple times, so it can divide the CPU time difference by the elapsed time between the calls to calculate a percentage. – Barmar Sep 03 '19 at 15:57
  • this behavior is documented in [psutils documentation](https://psutil.readthedocs.io/en/latest/#psutil.cpu_percent) , to get a meaningful result, follow @Barmar 's answer – James Li Sep 03 '19 at 16:32

1 Answers1

1

CPU percentage is calculated by dividing the difference in CPU time used between two calls by the elapsed time. Therefore, it can't calculate a percentage on the first call, since there's no time difference yet.

So call it once, wait a short time, then call it again.

import psutil # for the linux process data and cpu usage
import time # for sleep()

# Initial call to get base values
_ = list(psutil.process_iter()
# Delay so it can calculate a difference.
time.sleep(1)

#Creates a list to store the output of procInfo
theOneProcessList = list()

# Lists every process and it's cpu usage and appends it to theOneProcessList
for proc in psutil.process_iter(attrs=None, ad_value=None):
    procInfo = proc.as_dict(attrs=['name', 'cpu_percent'])
    theOneProcessList.append(procInfo)
print(theOneProcessList)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • that totally makes sense. I want to give it a try but vscode keeps complaining that os doesn't have the sleep method – Iverol Sep 03 '19 at 16:30
  • That's because it's in the `time` module. – Barmar Sep 03 '19 at 16:32
  • 1 second is too short for an observable figure. 3 or 5 seconds may be better. and exceptions such as ```NoSuchProcess``` is to be catched when doing long time loop – James Li Sep 03 '19 at 16:34
  • Right, utilities like `top` usually update every 5 or 10 seconds. – Barmar Sep 03 '19 at 16:36
  • still doesn't work. cpu_percent is still 0 for every process. I tried with a sleep of 10 seconds as well – Iverol Sep 03 '19 at 16:48
  • I pretty much gave up on the idea ended up using memory_percentage instead. It gives an output and will still work but not in the same way. – Iverol Sep 03 '19 at 19:40