2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2018-09-28 18:57:37 +00:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3:
|
2018-09-27 20:24:09 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
2018-09-12 20:46:35 +00:00
|
|
|
import psutil
|
2018-09-14 15:54:10 +00:00
|
|
|
import time
|
|
|
|
from threading import Thread
|
2018-09-13 21:24:55 +00:00
|
|
|
import operator
|
2018-09-12 20:46:35 +00:00
|
|
|
|
2018-09-14 15:54:10 +00:00
|
|
|
|
2018-09-12 20:46:35 +00:00
|
|
|
"""
|
|
|
|
PSUTIL Desktop Widget
|
|
|
|
Creates a floating CPU utilization window that is always on top of other windows
|
|
|
|
You move it by grabbing anywhere on the window
|
|
|
|
Good example of how to do a non-blocking, polling program using PySimpleGUI
|
|
|
|
Use the spinner to adjust the number of seconds between readings of the CPU utilizaiton
|
|
|
|
|
|
|
|
NOTE - you will get a warning message printed when you exit using exit button.
|
|
|
|
It will look something like:
|
|
|
|
invalid command name "1616802625480StopMove"
|
|
|
|
"""
|
|
|
|
|
2018-09-15 19:44:57 +00:00
|
|
|
# globale used to communicate with thread.. yea yea... it's working fine
|
2018-09-14 15:54:10 +00:00
|
|
|
g_interval = 1
|
|
|
|
g_cpu_percent = 0
|
|
|
|
g_procs = None
|
|
|
|
g_exit = False
|
|
|
|
|
|
|
|
def CPU_thread(args):
|
|
|
|
global g_interval, g_cpu_percent, g_procs, g_exit
|
|
|
|
|
|
|
|
while not g_exit:
|
2018-09-16 21:08:26 +00:00
|
|
|
try:
|
|
|
|
g_cpu_percent = psutil.cpu_percent(interval=g_interval)
|
|
|
|
g_procs = psutil.process_iter()
|
|
|
|
except:
|
|
|
|
pass
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global g_interval, g_procs, g_exit
|
|
|
|
|
|
|
|
# ---------------- Create Form ----------------
|
|
|
|
sg.ChangeLookAndFeel('Black')
|
2018-09-24 22:01:00 +00:00
|
|
|
layout = [[sg.Text('', size=(8,1), font=('Helvetica', 20),text_color=sg.YELLOWS[0], justification='center', key='text')],
|
2018-09-14 15:54:10 +00:00
|
|
|
[sg.Text('', size=(30, 8), font=('Courier New', 12),text_color='white', justification='left', key='processes')],
|
2018-09-15 19:44:57 +00:00
|
|
|
[sg.Exit(button_color=('white', 'firebrick4'), pad=((15,0), 0)), sg.Spin([x+1 for x in range(10)], 1, key='spin')],]
|
2018-09-14 15:54:10 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
window = sg.Window('CPU Utilization', no_titlebar=True, auto_size_buttons=False,
|
|
|
|
keep_on_top=True, grab_anywhere=True).Layout(layout)
|
|
|
|
|
2018-09-15 19:44:57 +00:00
|
|
|
# start cpu measurement thread
|
2018-09-14 15:54:10 +00:00
|
|
|
thread = Thread(target=CPU_thread,args=(None,))
|
|
|
|
thread.start()
|
|
|
|
# ---------------- main loop ----------------
|
|
|
|
while (True):
|
|
|
|
# --------- Read and update window --------
|
2018-09-24 22:01:00 +00:00
|
|
|
button, values = window.ReadNonBlocking()
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
# --------- Do Button Operations --------
|
|
|
|
if values is None or button == 'Exit':
|
|
|
|
break
|
|
|
|
try:
|
|
|
|
g_interval = int(values['spin'])
|
|
|
|
except:
|
|
|
|
g_interval = 1
|
|
|
|
|
2018-09-15 19:44:57 +00:00
|
|
|
# cpu_percent = psutil.cpu_percent(interval=interval) # if don't wan to use a task
|
2018-09-14 15:54:10 +00:00
|
|
|
cpu_percent = g_cpu_percent
|
2018-09-15 19:44:57 +00:00
|
|
|
|
|
|
|
# let the GUI run ever 700ms regardless of CPU polling time. makes window be more responsive
|
|
|
|
time.sleep(.7)
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
display_string = ''
|
|
|
|
if g_procs:
|
|
|
|
# --------- Create list of top % CPU porocesses --------
|
2018-09-16 21:08:26 +00:00
|
|
|
try:
|
|
|
|
top = {proc.name() : proc.cpu_percent() for proc in g_procs}
|
|
|
|
except: pass
|
|
|
|
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True)
|
|
|
|
if top_sorted:
|
|
|
|
top_sorted.pop(0)
|
|
|
|
display_string = ''
|
|
|
|
for proc, cpu in top_sorted:
|
2018-09-15 23:41:41 +00:00
|
|
|
display_string += '{:2.2f} {}\n'.format(cpu/10, proc)
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
# --------- Display timer in window --------
|
2018-09-24 22:01:00 +00:00
|
|
|
window.FindElement('text').Update('CPU {}'.format(cpu_percent))
|
|
|
|
window.FindElement('processes').Update(display_string)
|
2018-09-14 15:54:10 +00:00
|
|
|
|
|
|
|
# Broke out of main loop. Close the window.
|
2018-09-24 22:01:00 +00:00
|
|
|
window.CloseNonBlocking()
|
2018-09-14 15:54:10 +00:00
|
|
|
g_exit = True
|
|
|
|
thread.join()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|