From 8cca39bb1a63fc710edc85a3858862246cd368a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Mand=C3=A1k?= Date: Mon, 22 Jun 2015 12:27:58 +0200 Subject: [PATCH] StandaloneIO: Modules with above average intervals are refreshed in their own threads. This should prevent freezing of the bar caused by running all modules. --- i3pystatus/core/io.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/i3pystatus/core/io.py b/i3pystatus/core/io.py index 6c06b66..38fd43f 100644 --- a/i3pystatus/core/io.py +++ b/i3pystatus/core/io.py @@ -54,6 +54,7 @@ class StandaloneIO(IOHandler): refresh_cond = Condition() modules = [] + treshold_interval = 20.0 n = -1 proto = [ @@ -75,6 +76,7 @@ class StandaloneIO(IOHandler): StandaloneIO.modules = modules def read(self): + StandaloneIO.compute_treshold_interval() StandaloneIO.refresh_cond.acquire() while True: @@ -91,6 +93,11 @@ class StandaloneIO(IOHandler): return self.proto[min(self.n, len(self.proto) - 1)] + @classmethod + def compute_treshold_interval(cls): + intervals = [m.interval for m in cls.modules if hasattr(m, "interval")] + cls.treshold_interval = round(sum(intervals) / len(intervals)) + @classmethod def async_refresh(cls): cls.refresh_cond.acquire() @@ -99,10 +106,21 @@ class StandaloneIO(IOHandler): @staticmethod def refresh_signal_handler(signo, frame): - if signo == signal.SIGUSR1: - for module in StandaloneIO.modules: + if signo != signal.SIGUSR1: + return + + threads = [] + for module in StandaloneIO.modules: + if hasattr(module, "interval"): + if module.interval > StandaloneIO.treshold_interval: + thread = Thread(target=module.run) + thread.start() + else: + module.run() + else: module.run() - StandaloneIO.async_refresh() + + StandaloneIO.async_refresh() class JSONIO: