i3pystatus.core.threads ; some smaller changes.
This commit is contained in:
parent
2bad329d13
commit
3ebcbec4e1
@ -35,10 +35,10 @@ class IntervalModule(Module):
|
|||||||
|
|
||||||
def registered(self, status_handler):
|
def registered(self, status_handler):
|
||||||
if self.interval in IntervalModule.managers:
|
if self.interval in IntervalModule.managers:
|
||||||
IntervalModule.managers[self.interval].add(self)
|
IntervalModule.managers[self.interval].append(self)
|
||||||
else:
|
else:
|
||||||
am = AutomagicManager(self.interval)
|
am = AutomagicManager(self.interval)
|
||||||
am.add(self)
|
am.append(self)
|
||||||
IntervalModule.managers[self.interval] = am
|
IntervalModule.managers[self.interval] = am
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
|
@ -3,26 +3,22 @@ import sys
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
import collections
|
||||||
|
|
||||||
if hasattr(time, "perf_counter"):
|
if hasattr(time, "perf_counter"):
|
||||||
timer = time.perf_counter
|
timer = time.perf_counter
|
||||||
else:
|
else:
|
||||||
timer = time.clock
|
timer = time.clock
|
||||||
|
|
||||||
class StderrExcDelegate:
|
|
||||||
def __call__(self, exc):
|
|
||||||
traceback.print_exception(*sys.exc_info(), file=sys.stderr)
|
|
||||||
|
|
||||||
class ExceptionWrapper:
|
class ExceptionWrapper:
|
||||||
def __init__(self, workload, exc_delegate):
|
def __init__(self, workload):
|
||||||
self.workload = workload
|
self.workload = workload
|
||||||
self.exc_delegate = exc_delegate
|
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
try:
|
try:
|
||||||
self.workload()
|
self.workload()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.exc_delegate(exc)
|
traceback.print_exception(*sys.exc_info(), file=sys.stderr)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(self.workload)
|
return repr(self.workload)
|
||||||
@ -41,30 +37,30 @@ class WorkloadWrapper:
|
|||||||
return repr(self.workload)
|
return repr(self.workload)
|
||||||
|
|
||||||
class Thread(threading.Thread):
|
class Thread(threading.Thread):
|
||||||
def __init__(self, target_interval, workloads=None):
|
def __init__(self, target_interval, workloads=None, start_barrier=1):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.workloads = workloads if workloads is not None else []
|
self.workloads = workloads if workloads is not None else []
|
||||||
self.target_interval = target_interval
|
self.target_interval = target_interval
|
||||||
|
self.start_barrier = start_barrier
|
||||||
self.daemon = True
|
self.daemon = True
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self.workloads)
|
return iter(self.workloads)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.workloads)
|
return len(self.workloads)
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
return self.workloads.pop()
|
return self.workloads.pop()
|
||||||
|
def append(self, workload):
|
||||||
def push(self, workload):
|
|
||||||
self.workloads.append(workload)
|
self.workloads.append(workload)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.workloads:
|
while len(self) <= self.start_barrier:
|
||||||
for workload in self.workloads:
|
time.sleep(0.3)
|
||||||
workload()
|
|
||||||
|
|
||||||
|
while self:
|
||||||
|
for workload in self:
|
||||||
|
workload()
|
||||||
self.workloads.sort(key=lambda workload: workload.time)
|
self.workloads.sort(key=lambda workload: workload.time)
|
||||||
|
|
||||||
filltime = self.target_interval - self.time
|
filltime = self.target_interval - self.time
|
||||||
@ -73,7 +69,7 @@ class Thread(threading.Thread):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def time(self):
|
def time(self):
|
||||||
return sum(map(lambda workload: workload.time, self.workloads))
|
return sum(map(lambda workload: workload.time, self))
|
||||||
|
|
||||||
class AutomagicManager:
|
class AutomagicManager:
|
||||||
def __init__(self, target_interval):
|
def __init__(self, target_interval):
|
||||||
@ -83,7 +79,6 @@ class AutomagicManager:
|
|||||||
|
|
||||||
initial_thread = Thread(target_interval, [self.wrap(self)])
|
initial_thread = Thread(target_interval, [self.wrap(self)])
|
||||||
self.threads = [initial_thread]
|
self.threads = [initial_thread]
|
||||||
|
|
||||||
initial_thread.start()
|
initial_thread.start()
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
@ -95,7 +90,7 @@ class AutomagicManager:
|
|||||||
self.create_threads(self.partition(separate))
|
self.create_threads(self.partition(separate))
|
||||||
|
|
||||||
def wrap(self, workload):
|
def wrap(self, workload):
|
||||||
return WorkloadWrapper(ExceptionWrapper(workload, exc_delegate=StderrExcDelegate()))
|
return WorkloadWrapper(ExceptionWrapper(workload))
|
||||||
|
|
||||||
def optimize(self, thread, vtime):
|
def optimize(self, thread, vtime):
|
||||||
if len(thread) > 1 and vtime > self.upper_bound:
|
if len(thread) > 1 and vtime > self.upper_bound:
|
||||||
@ -110,12 +105,10 @@ class AutomagicManager:
|
|||||||
for workload in workloads:
|
for workload in workloads:
|
||||||
current_thread.append(workload)
|
current_thread.append(workload)
|
||||||
timesum += workload.time
|
timesum += workload.time
|
||||||
|
|
||||||
if timesum > self.lower_bound:
|
if timesum > self.lower_bound:
|
||||||
new_threads.append(current_thread)
|
new_threads.append(current_thread)
|
||||||
current_thread = []
|
current_thread = []
|
||||||
timesum = 0
|
timesum = 0
|
||||||
|
|
||||||
return new_threads
|
return new_threads
|
||||||
|
|
||||||
def create_threads(self, threads):
|
def create_threads(self, threads):
|
||||||
@ -123,9 +116,9 @@ class AutomagicManager:
|
|||||||
self.create_thread(workloads)
|
self.create_thread(workloads)
|
||||||
|
|
||||||
def create_thread(self, workloads):
|
def create_thread(self, workloads):
|
||||||
thread = Thread(self.target_interval, workloads)
|
thread = Thread(self.target_interval, workloads, start_barrier=0)
|
||||||
thread.start()
|
thread.start()
|
||||||
self.threads.append(thread)
|
self.threads.append(thread)
|
||||||
|
|
||||||
def add(self, workload):
|
def append(self, workload):
|
||||||
self.threads[0].push(self.wrap(workload))
|
self.threads[0].append(self.wrap(workload))
|
||||||
|
Loading…
Reference in New Issue
Block a user