From a3d86e7a44c58a00871adac2b7b067124c883999 Mon Sep 17 00:00:00 2001 From: enkore Date: Tue, 1 Oct 2013 13:04:00 +0200 Subject: [PATCH] core: remove threading package --- i3pystatus/core/threading.py | 1 + i3pystatus/core/threading/__init__.py | 2 - i3pystatus/core/threading/manager.py | 41 ------------------ i3pystatus/core/threading/threads.py | 61 --------------------------- i3pystatus/core/threading/wrapper.py | 34 --------------- 5 files changed, 1 insertion(+), 138 deletions(-) create mode 100644 i3pystatus/core/threading.py delete mode 100644 i3pystatus/core/threading/__init__.py delete mode 100644 i3pystatus/core/threading/manager.py delete mode 100644 i3pystatus/core/threading/threads.py delete mode 100644 i3pystatus/core/threading/wrapper.py diff --git a/i3pystatus/core/threading.py b/i3pystatus/core/threading.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/i3pystatus/core/threading.py @@ -0,0 +1 @@ + diff --git a/i3pystatus/core/threading/__init__.py b/i3pystatus/core/threading/__init__.py deleted file mode 100644 index cababbb..0000000 --- a/i3pystatus/core/threading/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ - -from i3pystatus.core.threading.manager import Manager diff --git a/i3pystatus/core/threading/manager.py b/i3pystatus/core/threading/manager.py deleted file mode 100644 index 6499ee3..0000000 --- a/i3pystatus/core/threading/manager.py +++ /dev/null @@ -1,41 +0,0 @@ - -from i3pystatus.core.util import partition -from i3pystatus.core.threading import threads, wrapper - -class Manager: - def __init__(self, target_interval): - self.target_interval = target_interval - self.upper_bound = target_interval * 1.1 - self.lower_bound = target_interval * 0.7 - - initial_thread = threads.Thread(target_interval, [self.wrap(self)]) - self.threads = [initial_thread] - - def __call__(self): - separate = [] - for thread in self.threads: - separate.extend(thread.branch(thread.time, self.upper_bound)) - self.create_threads(self.partition_workloads(separate)) - - def __repr__(self): - return "Manager" - - def wrap(self, workload): - return wrapper.WorkloadWrapper(wrapper.ExceptionWrapper(workload)) - - def partition_workloads(self, workloads): - return partition(workloads, self.lower_bound, lambda workload: workload.time) - - def create_threads(self, threads): - for workloads in threads: self.create_thread(workloads) - - def create_thread(self, workloads): - thread = threads.Thread(self.target_interval, workloads, start_barrier=0) - thread.start() - self.threads.append(thread) - - def append(self, workload): - self.threads[0].append(self.wrap(workload)) - - def start(self): - for thread in self.threads: thread.start() diff --git a/i3pystatus/core/threading/threads.py b/i3pystatus/core/threading/threads.py deleted file mode 100644 index 38eaf76..0000000 --- a/i3pystatus/core/threading/threads.py +++ /dev/null @@ -1,61 +0,0 @@ - -import time -import threading - -try: - from setproctitle import setproctitle -except ImportError: - def setproctitle(title): - pass - -class Thread(threading.Thread): - def __init__(self, target_interval, workloads=None, start_barrier=1): - super().__init__() - self.workloads = workloads or [] - self.target_interval = target_interval - self.start_barrier = start_barrier - self.daemon = True - - def __iter__(self): - return iter(self.workloads) - - def __len__(self): - return len(self.workloads) - - def pop(self): - return self.workloads.pop() - - def append(self, workload): - self.workloads.append(workload) - - @property - def time(self): - return sum(map(lambda workload: workload.time, self)) - - def wait_for_start_barrier(self): - while len(self) <= self.start_barrier: - time.sleep(0.4) - - def set_thread_title(self): - setproctitle("i3pystatus {name}: {workloads}".format( - name=self.name, - workloads=list(map(repr, self.workloads)) - )) - - def execute_workloads(self): - for workload in self: workload() - self.workloads.sort(key=lambda workload: workload.time) - - def run(self): - self.set_thread_title() - while self: - self.execute_workloads() - filltime = self.target_interval - self.time - if filltime > 0: - time.sleep(filltime) - - def branch(self, vtime, bound): - if len(self) > 1 and vtime > bound: - remove = self.pop() - return [remove] + self.branch(vtime - remove.time, bound) - return [] diff --git a/i3pystatus/core/threading/wrapper.py b/i3pystatus/core/threading/wrapper.py deleted file mode 100644 index 6c615a5..0000000 --- a/i3pystatus/core/threading/wrapper.py +++ /dev/null @@ -1,34 +0,0 @@ - -import sys -import time -import traceback -import threading - -timer = time.perf_counter if hasattr(time, "perf_counter") else time.clock - -class Wrapper: - def __init__(self, workload): - self.workload = workload - - def __repr__(self): - return repr(self.workload) - -class ExceptionWrapper(Wrapper): - def __call__(self): - try: - self.workload() - except: - sys.stderr.write("Exception in {thread} at {time}\n".format( - thread=threading.current_thread().name, - time=time.strftime("%c") - )) - traceback.print_exc(file=sys.stderr) - sys.stderr.flush() - -class WorkloadWrapper(Wrapper): - time = 0.0 - - def __call__(self): - tp1 = timer() - self.workload() - self.time = timer() - tp1