[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:
parent
a13898cbae
commit
547874dafd
2 changed files with 19 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
|
||||||
import core.config
|
import core.config
|
||||||
import core.input
|
import core.input
|
||||||
|
@ -58,6 +59,8 @@ class Module(core.input.Object):
|
||||||
|
|
||||||
def __init__(self, config=core.config.Config([]), theme=None, widgets=[]):
|
def __init__(self, config=core.config.Config([]), theme=None, widgets=[]):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.background = False
|
||||||
|
self.__thread = None
|
||||||
self.__config = config
|
self.__config = config
|
||||||
self.__widgets = widgets if isinstance(widgets, list) else [widgets]
|
self.__widgets = widgets if isinstance(widgets, list) else [widgets]
|
||||||
|
|
||||||
|
@ -116,14 +119,28 @@ class Module(core.input.Object):
|
||||||
def update(self):
|
def update(self):
|
||||||
pass
|
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
|
"""Wrapper method that ensures that all exceptions thrown by the
|
||||||
update() method are caught and displayed in a bumblebee_status.module.Error
|
update() method are caught and displayed in a bumblebee_status.module.Error
|
||||||
module
|
module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def update_wrapper(self):
|
def internal_update(self, trigger_redraw=False):
|
||||||
try:
|
try:
|
||||||
self.update()
|
self.update()
|
||||||
|
if trigger_redraw:
|
||||||
|
core.event.trigger("update", [self.id], redraw_only=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.set("interval", 1)
|
self.set("interval", 1)
|
||||||
module = Error(config=self.__config, module="error", error=str(e))
|
module = Error(config=self.__config, module="error", error=str(e))
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Module(core.module.Module):
|
||||||
def __init__(self, config, theme):
|
def __init__(self, config, theme):
|
||||||
super().__init__(config, theme, core.widget.Widget(self.github))
|
super().__init__(config, theme, core.widget.Widget(self.github))
|
||||||
|
|
||||||
|
self.background = True
|
||||||
self.__count = 0
|
self.__count = 0
|
||||||
self.__label = ""
|
self.__label = ""
|
||||||
self.__requests = requests.Session()
|
self.__requests = requests.Session()
|
||||||
|
|
Loading…
Reference in a new issue