Rename and rework of 2 multithreaded demos
This commit is contained in:
parent
9a7d7ac259
commit
79e02c5726
|
@ -35,22 +35,58 @@ import PySimpleGUI as sg
|
||||||
# ## ## ## ## ## ## ## ## ## ##
|
# ## ## ## ## ## ## ## ## ## ##
|
||||||
# ## ## ## ## ## ######## ## ## ########
|
# ## ## ## ## ## ######## ## ## ########
|
||||||
|
|
||||||
def worker_thread(thread_name, run_freq, gui_queue):
|
def worker_thread1(thread_name, run_freq, gui_queue):
|
||||||
"""
|
"""
|
||||||
A worker thrread that communicates with the GUI
|
A worker thread that communicates with the GUI
|
||||||
These threads can call functions that block withouth affecting the GUI (a good thing)
|
These threads can call functions that block without affecting the GUI (a good thing)
|
||||||
Note that this function is the code started as each thread. All threads are identical in this way
|
Note that this function is the code started as each thread. All threads are identical in this way
|
||||||
:param thread_name: Text name used for displaying info
|
:param thread_name: Text name used for displaying info
|
||||||
:param run_freq: How often the thread should run in milliseconds
|
:param run_freq: How often the thread should run in milliseconds
|
||||||
:param gui_queue: Queue used to communicate with the GUI
|
:param gui_queue: Queue used to communicate with the GUI
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
print('Starting thread - {} that runs every {} ms'.format(thread_name, run_freq))
|
print('Starting thread 1 - {} that runs every {} ms'.format(thread_name, run_freq))
|
||||||
for i in itertools.count(): # loop forever, keeping count in i as it loops
|
for i in itertools.count(): # loop forever, keeping count in i as it loops
|
||||||
time.sleep(run_freq/1000) # sleep for a while
|
time.sleep(run_freq/1000) # sleep for a while
|
||||||
# put a message into queue for GUI
|
# put a message into queue for GUI
|
||||||
gui_queue.put('{} - {}'.format(thread_name, i))
|
gui_queue.put('{} - {}'.format(thread_name, i))
|
||||||
|
|
||||||
|
|
||||||
|
def worker_thread2(thread_name, run_freq, gui_queue):
|
||||||
|
"""
|
||||||
|
A worker thread that communicates with the GUI
|
||||||
|
These threads can call functions that block without affecting the GUI (a good thing)
|
||||||
|
Note that this function is the code started as each thread. All threads are identical in this way
|
||||||
|
:param thread_name: Text name used for displaying info
|
||||||
|
:param run_freq: How often the thread should run in milliseconds
|
||||||
|
:param gui_queue: Queue used to communicate with the GUI
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
print('Starting thread 2 - {} that runs every {} ms'.format(thread_name, run_freq))
|
||||||
|
for i in itertools.count(): # loop forever, keeping count in i as it loops
|
||||||
|
time.sleep(run_freq/1000) # sleep for a while
|
||||||
|
# put a message into queue for GUI
|
||||||
|
gui_queue.put('{} - {}'.format(thread_name, i))
|
||||||
|
|
||||||
|
|
||||||
|
def worker_thread3(thread_name, run_freq, gui_queue):
|
||||||
|
"""
|
||||||
|
A worker thread that communicates with the GUI
|
||||||
|
These threads can call functions that block without affecting the GUI (a good thing)
|
||||||
|
Note that this function is the code started as each thread. All threads are identical in this way
|
||||||
|
:param thread_name: Text name used for displaying info
|
||||||
|
:param run_freq: How often the thread should run in milliseconds
|
||||||
|
:param gui_queue: Queue used to communicate with the GUI
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
print('Starting thread 3 - {} that runs every {} ms'.format(thread_name, run_freq))
|
||||||
|
for i in itertools.count(): # loop forever, keeping count in i as it loops
|
||||||
|
time.sleep(run_freq/1000) # sleep for a while
|
||||||
|
# put a message into queue for GUI
|
||||||
|
gui_queue.put('{} - {}'.format(thread_name, i))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ###### ## ## ####
|
# ###### ## ## ####
|
||||||
# ## ## ## ## ##
|
# ## ## ## ## ##
|
||||||
# ## ## ## ##
|
# ## ## ## ##
|
||||||
|
@ -109,12 +145,12 @@ if __name__ == '__main__':
|
||||||
# -- Create a Queue to communicate with GUI --
|
# -- Create a Queue to communicate with GUI --
|
||||||
# queue used to communicate between the gui and the threads
|
# queue used to communicate between the gui and the threads
|
||||||
gui_queue = queue.Queue()
|
gui_queue = queue.Queue()
|
||||||
# -- Start worker threads, one runs twice as often as the other
|
# -- Start worker threads, each taking a different amount of time
|
||||||
threading.Thread(target=worker_thread, args=(
|
threading.Thread(target=worker_thread1, args=(
|
||||||
'Thread 1', 500, gui_queue,), daemon=True).start()
|
'Thread 1', 500, gui_queue,), daemon=True).start()
|
||||||
threading.Thread(target=worker_thread, args=(
|
threading.Thread(target=worker_thread2, args=(
|
||||||
'Thread 2', 200, gui_queue,), daemon=True).start()
|
'Thread 2', 200, gui_queue,), daemon=True).start()
|
||||||
threading.Thread(target=worker_thread, args=(
|
threading.Thread(target=worker_thread3, args=(
|
||||||
'Thread 3', 1000, gui_queue,), daemon=True).start()
|
'Thread 3', 1000, gui_queue,), daemon=True).start()
|
||||||
# -- Start the GUI passing in the Queue --
|
# -- Start the GUI passing in the Queue --
|
||||||
the_gui(gui_queue)
|
the_gui(gui_queue)
|
|
@ -61,8 +61,7 @@ def the_gui():
|
||||||
[sg.Text('Click Go to start a long-running function call')],
|
[sg.Text('Click Go to start a long-running function call')],
|
||||||
[sg.Text('', size=(25, 1), key='-OUTPUT-')],
|
[sg.Text('', size=(25, 1), key='-OUTPUT-')],
|
||||||
[sg.Text('', size=(25, 1), key='-OUTPUT2-')],
|
[sg.Text('', size=(25, 1), key='-OUTPUT2-')],
|
||||||
[sg.Graph((10, 10), (0, 0), (10, 10),
|
[sg.Text(' ',size=(2,1), background_color='blue', key=i) for i in range(20)],
|
||||||
background_color='black', key=i) for i in range(20)],
|
|
||||||
[sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ]
|
[sg.Button('Go'), sg.Button('Popup'), sg.Button('Exit')], ]
|
||||||
|
|
||||||
window = sg.Window('Multithreaded Window', layout)
|
window = sg.Window('Multithreaded Window', layout)
|
||||||
|
@ -102,7 +101,7 @@ def the_gui():
|
||||||
window[completed_work_id].update(background_color='green')
|
window[completed_work_id].update(background_color='green')
|
||||||
|
|
||||||
if event == 'Popup':
|
if event == 'Popup':
|
||||||
sg.popup('This is a popup showing that the GUI is running')
|
sg.popup_non_blocking('This is a popup showing that the GUI is running', grab_anywhere=True)
|
||||||
# if user exits the window, then close the window and exit the GUI func
|
# if user exits the window, then close the window and exit the GUI func
|
||||||
window.close()
|
window.close()
|
||||||
|
|
Loading…
Reference in New Issue