[core/module] allow modules to perform updates in background

a module can now set `self.background = True` in its `__init__()` method
to make sure its update method is invoked in a separate thread.

also, do a PoC implementation of this for the github module.

TODO: add this to dev doc

see #640
This commit is contained in:
tobi-wan-kenobi 2020-05-30 17:20:41 +02:00
parent a13898cbae
commit 547874dafd
2 changed files with 19 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import os
import importlib
import logging
import threading
import core.config
import core.input
@ -58,6 +59,8 @@ class Module(core.input.Object):
def __init__(self, config=core.config.Config([]), theme=None, widgets=[]):
super().__init__()
self.background = False
self.__thread = None
self.__config = config
self.__widgets = widgets if isinstance(widgets, list) else [widgets]
@ -116,14 +119,28 @@ class Module(core.input.Object):
def update(self):
pass
def update_wrapper(self):
if self.background == True:
if self.__thread and self.__thread.isAlive():
return # skip this update interval
self.__thread = threading.Thread(target=self.internal_update, args=(True,))
self.__thread.start()
else:
self.internal_update(False)
"""Wrapper method that ensures that all exceptions thrown by the
update() method are caught and displayed in a bumblebee_status.module.Error
module
"""
def update_wrapper(self):
def internal_update(self, trigger_redraw=False):
try:
self.update()
if trigger_redraw:
core.event.trigger("update", [self.id], redraw_only=True)
except Exception as e:
self.set("interval", 1)
module = Error(config=self.__config, module="error", error=str(e))

View file

@ -34,6 +34,7 @@ class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.github))
self.background = True
self.__count = 0
self.__label = ""
self.__requests = requests.Session()