Better exception handling for both event loop and for when processes exit
This commit is contained in:
parent
8fab8d124e
commit
51f0bbd7a3
|
@ -42,36 +42,48 @@ def main():
|
||||||
|
|
||||||
# start cpu measurement thread
|
# start cpu measurement thread
|
||||||
# using the PySimpleGUI call to start and manage the thread
|
# using the PySimpleGUI call to start and manage the thread
|
||||||
window.perform_long_operation(lambda: CPU_thread(window), '-THREAD FINISHED-')
|
window.start_thread(lambda: CPU_thread(window), '-THREAD FINISHED-')
|
||||||
g_interval = 1
|
g_interval = 1
|
||||||
# ---------------- main loop ----------------
|
# Unusual construct of a Try around entire event loop... something is crashing, we need to find out what...
|
||||||
while True:
|
try:
|
||||||
# --------- Read and update window --------
|
# ---------------- main loop ----------------
|
||||||
event, values = window.read()
|
while True:
|
||||||
# print(event, values)
|
# --------- Read and update window --------
|
||||||
# --------- Do Button Operations --------
|
event, values = window.read()
|
||||||
if event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'):
|
# print(event, values)
|
||||||
sg.user_settings_set_entry('-location-', window.current_location()) # save window location before exiting
|
# --------- Do Button Operations --------
|
||||||
break
|
if event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'):
|
||||||
if event == 'Edit Me':
|
sg.user_settings_set_entry('-location-', window.current_location()) # save window location before exiting
|
||||||
sp = sg.execute_editor(__file__)
|
break
|
||||||
elif event == 'Version':
|
if event == 'Edit Me':
|
||||||
sg.popup_scrolled(__file__, sg.get_versions(), keep_on_top=True, location=window.current_location())
|
sp = sg.execute_editor(__file__)
|
||||||
elif event == '-CPU UPDATE FROM THREAD-': # indicates data from the thread has arrived
|
elif event == 'Version':
|
||||||
cpu_percent, procs = values[event] # the thread sends a tuple
|
sg.popup_scrolled(__file__, sg.get_versions(), keep_on_top=True, location=window.current_location())
|
||||||
if procs:
|
elif event == '-CPU UPDATE FROM THREAD-': # indicates data from the thread has arrived
|
||||||
# --------- Create dictionary of top % CPU processes. Format is name:cpu_percent --------
|
cpu_percent, procs = values[event] # the thread sends a tuple
|
||||||
top = {proc.name(): proc.cpu_percent() for proc in procs}
|
if procs:
|
||||||
|
# --------- Create dictionary of top % CPU processes. Format is name:cpu_percent --------
|
||||||
|
top = {}
|
||||||
|
for proc in procs:
|
||||||
|
try:
|
||||||
|
top[proc.name()] = proc.cpu_percent()
|
||||||
|
except Exception as e:
|
||||||
|
pass # it's OK to get an exception here because processes come and go... one may have gone...
|
||||||
|
# sg.Print('*** GOT Exception looping through procs ***', c='white on red', font='_ 18')
|
||||||
|
# sg.Print('Exception = ', e, 'procs=', procs, 'proc', proc)
|
||||||
|
|
||||||
top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True) # reverse sort to get highest CPU usage on top
|
top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True) # reverse sort to get highest CPU usage on top
|
||||||
if top_sorted:
|
if top_sorted:
|
||||||
top_sorted.pop(0) # remove the idle process
|
top_sorted.pop(0) # remove the idle process
|
||||||
display_string = '\n'.join([f'{cpu/10:2.2f} {proc:23}' for proc, cpu in top_sorted])
|
display_string = '\n'.join([f'{cpu/10:2.2f} {proc:23}' for proc, cpu in top_sorted])
|
||||||
# --------- Display timer and proceses in window --------
|
# --------- Display timer and proceses in window --------
|
||||||
window['-CPU PERCENT-'].update(f'CPU {cpu_percent}')
|
window['-CPU PERCENT-'].update(f'CPU {cpu_percent}')
|
||||||
window['-PROCESSES-'].update(display_string)
|
window['-PROCESSES-'].update(display_string)
|
||||||
# get the timeout from the spinner
|
# get the timeout from the spinner
|
||||||
g_interval = int(values['-SPIN-'])
|
g_interval = int(values['-SPIN-'])
|
||||||
|
except Exception as e:
|
||||||
|
sg.Print('*** GOT Exception in event loop ***', c='white on red', font='_ 18')
|
||||||
|
sg.Print('Exception = ', e, wait=True) # IMPORTANT to add a wait/blocking so that the print pauses execution. Otherwise program continue and exits
|
||||||
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue