now threading is functional, but plugins are suddenly way to huge

This commit is contained in:
Von Random 2016-10-22 05:50:11 +03:00
parent d77dbeb1de
commit c272a21aa7
5 changed files with 55 additions and 48 deletions

View file

@ -1,10 +1,18 @@
import time
import threading
def execute(config, section):
fmt = config.get(section, 'format')
result = dict()
if config.has_option(section, 'color'):
result['color'] = config.get(section, 'color')
result['full_text'] = time.strftime(fmt)
return result
class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
self.date_format = config.get(section, 'format')
self.status = dict()
if config.has_option(section, 'color'):
self.status['color'] = config.get(section, 'color')
self.freq = 1
def run(self):
while True:
self.status['full_text'] = time.strftime(self.date_format)
time.sleep(self.freq)

View file

@ -4,22 +4,20 @@ import time
import threading
class ConnTest(threading.Thread):
def __init__(self, threadID, name):
class PluginThread(threading.Thread):
def __init__(self, section, config, thread_id):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.hosts = list()
self.status = 'n/a'
self.freq = int()
self.threadID = thread_id
self.hosts = config.get(section, 'hosts').split(',')
self.title = config.get(section, 'title')
self.status = dict()
self.freq = 5
self.format_status('n/a')
def configure(self, hosts, freq=5):
self.hosts = hosts.split(',')
self.freq = freq
def format_status(self, state):
self.status['full_text'] = self.title + ': ' + state
def run(self):
global is_running
is_running = True
while True:
random.shuffle(self.hosts)
try:
@ -27,23 +25,10 @@ class ConnTest(threading.Thread):
fping = 'fping -q -c1 -t100 ' + host + ' &>/dev/null'
response = os.system(fping)
if response == 0:
self.status = 'on'
self.format_status('on')
break
self.status = 'off'
self.format_status('off')
except (KeyboardInterrupt, SystemExit):
break
time.sleep(self.freq)
is_running = False
ping_object = ConnTest(1, 'test_network')
def execute(config, section):
result = dict()
global ping_object
if is_running is False:
ping_object.configure(config.get(section, 'hosts'), 10)
ping_object.start()
result['full_text'] = config.get(section, 'title') + ' ' + ping_object.status
return result