updates to use tk after for backgrounded tasks, also added background task convenience class for running something in the background and running a callback using tk.after when done

This commit is contained in:
Blake Harnden 2019-12-30 16:34:44 -08:00
parent dd43fae62a
commit 3e87737ee6
7 changed files with 127 additions and 110 deletions

29
daemon/core/gui/task.py Normal file
View file

@ -0,0 +1,29 @@
import logging
import threading
class BackgroundTask:
def __init__(self, master, task, callback=None, args=()):
self.master = master
self.args = args
self.task = task
self.callback = callback
self.thread = None
def start(self):
logging.info("starting task")
self.thread = threading.Thread(target=self.run, daemon=True)
self.thread.start()
def run(self):
result = self.task(*self.args)
logging.info("task completed")
if self.callback:
if result is None:
args = ()
elif isinstance(result, (list, tuple)):
args = result
else:
args = (result,)
logging.info("calling callback: %s", args)
self.master.after(0, self.callback, *args)