[modules/ping] Spawn thread on-the-fly

Instead of having a thread that runs in the background continuously,
spawn a new one for every update interval. That speeds up the tests
quite a lot.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-11 11:50:15 +01:00
parent 71582cbcd7
commit 17ee621a5a

View file

@ -20,24 +20,11 @@ import bumblebee.output
import bumblebee.engine
def get_rtt(module, widget):
main = None
for thread in threading.enumerate():
if thread.name == "MainThread":
main = thread
interval = widget.get("interval")
next_check = 0
while main.is_alive():
try:
if int(time.time()) < next_check:
time.sleep(1)
continue
widget.set("rtt-unreachable", False)
res = bumblebee.util.execute("ping -n -q -c {} -W {} {}".format(
widget.get("rtt-probes"), widget.get("rtt-timeout"), widget.get("address")
))
next_check = int(time.time()) + interval
for line in res.split("\n"):
if not line.startswith("rtt"): continue
@ -62,8 +49,7 @@ class Module(bumblebee.engine.Module):
widget.set("rtt-avg", 0.0)
widget.set("rtt-unit", "")
self._thread = threading.Thread(target=get_rtt, args=(self,widget,))
self._thread.start()
self._next_check = 0
def rtt(self, widget):
if widget.get("rtt-unreachable"):
@ -78,4 +64,11 @@ class Module(bumblebee.engine.Module):
if widget.get("rtt-unreachable"): return ["critical"]
return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0)
def update(self, widgets):
if int(time.time()) < self._next_check:
return
thread = threading.Thread(target=get_rtt, args=(self,widgets[0],))
thread.start()
self._next_check = int(time.time()) + widgets[0].get("interval")
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4