我受到类似 github 项目的启发,尝试制作文字云壁纸生成器。我经验不多,这似乎是一个很好的初学者项目。
目前,我正在尝试从
psutil
中获取一些可用的输出,它似乎确实有效,但有些地方不太对劲。
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)
输出最终是一个数组,列出了 linux 中的每个进程及其相应的 cpu 使用情况。像这样的东西:
[{'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}]
但由于某种原因,每个进程的 cpu 使用率始终为 0。 这对于词云来说是行不通的。它们的大小差异因 cpu 使用值而异。
我做错了什么吗?有没有一种真正有效的替代方法?
CPU 百分比的计算方法是将两次调用之间使用的 CPU 时间差除以经过的时间。因此,它无法计算第一次调用的百分比,因为还没有时间差。
所以调用一次,等待一小会儿,然后再次调用。
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)