3.12: More bullet-proof wrapper

This commit is contained in:
enkore 2013-03-10 21:38:02 +01:00
parent b42cd6aa15
commit 0a32494ce8
3 changed files with 8 additions and 8 deletions

View File

@ -14,7 +14,7 @@ class Manager:
def __call__(self): def __call__(self):
separate = [] separate = []
for thread in self.threads: for thread in self.threads:
separate.extend(self.branch(thread, thread.time)) separate.extend(thread.branch(thread.time, self.upper_bound))
self.create_threads(self.partition(separate)) self.create_threads(self.partition(separate))
def __repr__(self): def __repr__(self):
@ -23,12 +23,6 @@ class Manager:
def wrap(self, workload): def wrap(self, workload):
return wrapper.WorkloadWrapper(wrapper.ExceptionWrapper(workload)) return wrapper.WorkloadWrapper(wrapper.ExceptionWrapper(workload))
def branch(self, thread, vtime):
if len(thread) > 1 and vtime > self.upper_bound:
remove = thread.pop()
return [remove] + self.branch(thread, vtime - remove.time)
return []
def partition(self, workloads): def partition(self, workloads):
return partition(workloads, self.lower_bound, lambda workload: workload.time) return partition(workloads, self.lower_bound, lambda workload: workload.time)

View File

@ -51,3 +51,9 @@ class Thread(threading.Thread):
filltime = self.target_interval - self.time filltime = self.target_interval - self.time
if filltime > 0: if filltime > 0:
time.sleep(filltime) 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 []

View File

@ -16,7 +16,7 @@ class ExceptionWrapper(Wrapper):
def __call__(self): def __call__(self):
try: try:
self.workload() self.workload()
except Exception as exc: except BaseException as exc:
sys.stderr.write("Exception in {thread}".format(thread=threading.current_thread().name)) sys.stderr.write("Exception in {thread}".format(thread=threading.current_thread().name))
traceback.print_exception(*sys.exc_info(), file=sys.stderr) traceback.print_exception(*sys.exc_info(), file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()